2017-08-14 84 views
0

概述:我需要更新包含任何新图像链接的图像链接的数组。同时,我将所有先前上传的图像保存在数组中。我的问题是,这样做时,以前的图像链接得到组合。下面的例子。我将如何更改我的代码来修复数组?谢谢你的帮助。Javascript - 循环访问数组

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成独立的部分阵列。 (我认为。)任何帮助或建议非常感谢!

+1

那输出简直不可能?如果'allCurrentImages'是一个数组,并且它有两个图像,则该条件永远不会运行,因为在条件运行之前数组需要长度为3或更长的时间? – adeneo

+0

@adeneo我可能是错的,但我认为.length正在检查文本中的字符数而不是数组中的对象。有两张图片的长度是48 – AndrewLeonardi

+0

@ 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 – AndrewLeonardi

回答

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); 
    } 

}); 
0

变化仅仅是你在这段代码中,删除也是针对遍历字符串的所有字符,它应该超过2幅图像,即使工作:

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

看起来像这样可以工作,但它总是冻结第二张图片上的网站。我注意到split(',')导致它像这样分裂,我认为这是一个关键问题:u,p,l,o,a,d,s,/,5,9,8,女,4,C,C,0,9,8,3,E,F,7,1,1,0,一个,d,2,7-,E,E,6,1,5,0,2, 7,4,8,3,5,9,9,9,2 – AndrewLeonardi

+0

您需要删除for,否则您将遍历字符串的所有字符,而不是所有图像。更新了代码 – quirimmo

1

可以前把它分解:

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

for (i=0;i<allCurrentImagesTmp .length;i++){ 
    allCurrentImages.concat(allCurrentImagesTmp[i].split(",")); 
} 
... 
// your code 
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. 
+0

出于某种原因,它分裂如下:u,p,l,o,a,d,s,/,5,9,8,f,4,c,c,0,9,8,3, E,F,7,1,1,0,一个,d,2,7-,E,E,6,1,5,0,2,7,4,8,3,5,9,9,9, 2 – AndrewLeonardi

+0

@AndrewLeonardi - 它看起来好像req.body.oldimages可能只是一个字符串,而不是一个字符串数组。看到我上面的修订。 – LHM

1

嗯我真的不知道你的目的,我明白了一些时间你有串&阵列的某个时候,你想在另一个数组的开头添加元素......我想这样做正确的&正确方法简单的东西:

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. 

此代码应工作,但只有当图像没有“,”在他们的名字,如果你想控制这个,你将不得不阻止在前端&后端与正则表达式。