Javascript - 遍历数组

Posted

技术标签:

【中文标题】Javascript - 遍历数组【英文标题】:Javascript - Looping through an array 【发布时间】:2018-01-22 19:39:02 【问题描述】:

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

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

【问题讨论】:

那输出根本不可能?如果allCurrentImages 是一个数组,并且它有两个图像,则条件永远不会运行,因为在条件运行之前数组的长度需要为 3 或更多? @adeneo 我可能是错的,但我认为 .length 是检查文本中的字符数而不是数组中的对象。两张图片的长度为 48 @adeneo 在拆分时也会像下面这样拆分。知道为什么吗? u,p,l,o,a,d,s,/,5,9,8,f,4,c,c,0,9,8,3,e,f,7,1,1,0, a,d,2,7,e,e,6,1,5,0,2,7,4,8,3,5,9,9,9,2 那么它是一个字符串吗?问题是,如果它是一个字符串,你也不会得到那个输出,你在迭代时得到 uploads/598f4cc,uploads/53eew2w 作为单个项目的唯一方法是,如果 req.body.oldimages 实际上是一个数组,否则它会像这样 -> jsfiddle.net/x11dxdzj/2 @adeneo 非常感谢您的帮助。您的洞察力帮助我找出了问题 【参考方案1】:

之前可以拆分:

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

for (i=0;i<allCurrentImagesTmp .length;i++)
  allCurrentImages.concat(allCurrentImagesTmp[i].split(","));

...
// your code

【讨论】:

【参考方案2】:

嗯,我不太确定你的目标,我知道有时你得到字符串和有时数组,你想在另一个数组的开头添加元素......我认为正确和正确的方法是一些简单的事情:

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.

此代码应该有效,但前提是图像的名称中没有“,”,如果您想控制它,您必须使用正则表达式来防止前端和后端。

【讨论】:

【参考方案3】:

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.

【讨论】:

由于某种原因,它像这样分裂:u,p,l,o,a,d,s,/,5,9,8,f,4,c,c,0,9, 8,3,e,f,7,1,1,0,a,d,2,7,e,e,6,1,5,0,2,7,4,8,3,5,9, 9,9,2 @AndrewLeonardi - 看起来 req.body.oldimages 可能只是一个字符串,而不是字符串数组。请参阅我上面的修订。【参考方案4】:

只需在此更改您的代码,同时删除字符串的所有字符的 for which 循环,它甚至应该适用于超过 2 个图像:

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

【讨论】:

看起来这应该可以,但它总是将网站冻结在第二张图片上。我注意到 split(',') 导致它像这样分裂,我打赌这是一个关键问题:u,p,l,o,a,d,s,/,5,9,8, f,4,c,c,0,9,8,3,e,f,7,1,1,0,a,d,2,7,e,e,6,1,5,0,2, 7,4,8,3,5,9,9,9,2 您需要删除 for 否则您将遍历字符串的所有字符,而不是所有图像。更新了代码【参考方案5】:

问题在于 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);
    

);

【讨论】:

以上是关于Javascript - 遍历数组的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 遍历出一个数组中对象的某个值

关于javascript的一个数组问题!如何遍历ul(多组)下面的li的图片src地址组成数组?

javascript数组常用的遍历方法

使用 javascript 遍历数组 [重复]

遍历包含另一个数组javascript的数组

Javascript - 遍历数组