繁体   English   中英

Javascript-遍历数组

[英]Javascript - Looping through an array

概述:我需要用任何新的图像链接更新一个包含图像链接的数组。 同时,我将所有以前上传的图像保留在数组中。 我的问题是,这样做时,以前的图像链接会合并在一起。 下面的例子。 我将如何更改代码以修复阵列? 谢谢你的帮助。

    var allimages = []

    var allCurrentImages = req.body.oldimages
    //this pulls all the previous image links

        if (allCurrentImages && allCurrentImages.length > 2){
         for (i=0;i<allCurrentImages.length;i++){
         allimages.push(allCurrentImages[i]);
         }
    }

    if (filepath && filepath.length > 2){
    allimages.push(filepath);
    }

问题

这是问题所在。 如果var allCurrentImages中包含两个图像,则该数组会将它们合并为一项,因为我正在请求主体。 当有3张图片时,它看起来像这样:

images[0] =  uploads/598f4cc,uploads/53eew2w
images[1] =  uploads/7wusjw2w

它需要看起来像这样:

images[0] = uploads/598f4cc
images[1] = uploads/53eew2w
images[2] = uploads/7wusjw2w

因此,我需要以某种方式将req.body.oldimages拆分为单独的部分,然后再将其推入数组。 (我认为。)任何帮助或建议,我们将不胜感激!

您可以先拆分它:

var allCurrentImagesTmp = req.body.oldimages
var allCurrentImages = [];

for (i=0;i<allCurrentImagesTmp .length;i++){
  allCurrentImages.concat(allCurrentImagesTmp[i].split(","));
}
...
// your code

req.body.oldimages是字符串数组吗? 如果是这样,您应该可以通过以下方式更改代码行来实现所需的功能:

allimages.push(allCurrentImages[i]);

对此:

allimages.push(allCurrentImages[i].split(','));

否则,因为它似乎可能是一个长字符串,所以您可以尝试一种更精确的方法来专门查找逗号并利用该信息来发挥自己的优势:

var CurrentImages = allCurrentImages; // Use temp variable to protect original
var CommaIndex = CurrentImages.indexOf(','); // Find index of first comma
while (CommaIndex>0) {  // If there is no comma present, indexOf returns -1
    allimages.push(CurrentImages.substring(0, CommaIndex-1)); // Push the first image path to allimages
    CurrentImages = CurrentImages.substring(CommaIndex+1, CurrentImages.length-1); // Grab the rest of the string after the first comma
    CommaIndex = CurrentImages.indexOf(','); // Check for another comma
}
allimages.push(CurrentImages);  // This pushes the final one after the last comma - or the only one if there was no comma.

嗯,我不太确定您的目标,我知道有时候您会收到字符串和某个数组,并且您想在另一个数组的开头添加元素...我认为正确而正确的方法很简单,例如:

let allimages = []

let allCurrentImages = req.body.oldimages.split(',');
//Split by coma

allimages = allimages.concat(allCurrentImages);
// attention contact return the concat array so you have to set it to a variable.

该代码应该有效,但是仅当图像名称中没有“,”时,如果要控制此代码,则必须使用正则表达式在前端和后端进行阻止。

只需在其中更改代码,还删除for循环遍历字符串的所有字符,它甚至应能用于2个以上的图像:

allimages.concat(allCurrentImages.split(','));

问题是Mongoose模型“ oldimages”是一个数组,但是用EJS打印出来就作为字符串打印出来了。 我避免了打印出EJS并从foundListings.currentimages中拉出数组来解决这个问题。

感谢您的所有帮助。

    Listings.findById(req.params.id, function(err, foundListings){

     var allimages = []

    var allCurrentImages = foundListings.currentimages;
    console.log('all images' + allCurrentImages)


        if (allCurrentImages){
         for (i=0;i<allCurrentImages.length;i++){
         allimages.push(allCurrentImages[i]);
         }
    }

    if (filepath && filepath.length > 2){
    allimages.push(filepath);
    }

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM