Javascript - 循环遍历数组

时间:2017-08-14 21:38:32

标签: javascript jquery arrays node.js mongoose

概述:我需要更新包含带有任何新图片链接的图片链接的数组。与此同时,我将所有以前上传的图像保存在阵列中。我的问题是,在执行此操作时,先前的图像链接会合并。以下示例。我如何更改代码来修复数组?谢谢你的帮助。

    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拆分为单独的部分,然后再将其推送到数组中。 (我想。)非常感谢任何帮助或建议!

5 个答案:

答案 0 :(得分:1)

您可以在之前拆分它:

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

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

答案 1 :(得分:1)

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.

答案 2 :(得分:1)

哼哼我不太确定你的目标,我知道有一段时间你得到了弦乐&amp; amp; sometime Array,你想在另一个数组的开头添加元素...我认为是正确的&amp;这样做的正确方法很简单:

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.

此代码应该有效但仅当图像没有&#34;,&#34;在他们的名字中,如果你想控制它,你将不得不在前端和前端防止后端与正则表达式。

答案 3 :(得分:0)

简单地改变你的代码,删除字符串所有字符的循环,它甚至可以用于2个以上的图像:

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

答案 4 :(得分:0)

问题是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);
    }

});
相关问题