2017-08-14 87 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. 

此代碼應工作,但只有當圖像沒有「,」在他們的名字,如果你想控制這個,你將不得不阻止在前端&後端與正則表達式。