简体   繁体   中英

Javascript - Looping through an array

Overview: I'm needing to update an array that contains image links with any new image links. At the same time I'm keeping all previously uploaded images in the array. My problem is that while doing this the previous image links get combined. Example below. How would I change my code to fix the array? Thanks for any help.

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

PROBLEM

Here's the problem. If var allCurrentImages has two images in it the array combines them into one item because I'm requesting the body. It looks like this when there are 3 images:

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

It needs to look like this:

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

So I need to somehow split the req.body.oldimages into separate parts before pushing it to the array. (I think.) Any help or advice is greatly appreciated!

You can split it before:

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

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

Is the req.body.oldimages an array of strings? If so, you should be able to achieve what you are looking for by changing a line to your code from this:

allimages.push(allCurrentImages[i]);

to this:

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

Otherwise, since it seems that it may be one long string, you could try a more precise method of looking for the commas specifically and using that information to your advantage:

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.

Hum I am not really sure about your aim, I understand that some time you got string & sometime Array and you want to add element at the beginning of another array... I think a correct & proper way to do this is something simple as:

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.

This code should work but only if images does not have "," in their name, if you want to control this you will have to prevent in front-end & backend with a regex.

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

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

The issue was the Mongoose model "oldimages" was an array but printing it outwith EJS printed it out as a string. I got around this by avoiding printing out the EJS and pulled the array from foundListings.currentimages.

Thanks for all of the help.

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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