简体   繁体   English

如何在javascript中将Array值与'for'循环的结果进行比较

[英]How to compare Array value to result of 'for' loop in javascript

I have an empty array (called zoomthumbsarray) which gets values pushed to it whilst a 'for' loop is running. 我有一个空数组(称为zoomthumbsarray),当'for'循环运行时,它会将值推送到它。 This 'for' loop is checking if a thumbnail image is present in the backend against the particular product the user is viewing. 这个'for'循环检查后端是否存在缩略图图像,而不是用户正在查看的特定产品。 If there is an image it gets added into a vertical slider. 如果有图像,则会将其添加到垂直滑块中。 The current issue is there are non colour specific images (like lifestyle shots) that are being added into the slider multiple times. 目前的问题是有多种非颜色特定的图像(如生活方式照片)被多次添加到滑块中。

So I need to check if the image found in the for loop is currently stored in the array. 所以我需要检查for循环中找到的图像当前是否存储在数组中。 If it is present, the image has already been generated and I don't want it to get pulled into the slider again. 如果它存在,图像已经生成,我不希望它再次被拉入滑块。 If it hasn't then the image will get added. 如果没有,那么图像将被添加。

Below is the code I am working on. 以下是我正在处理的代码。 I would presume indexOf would be used but can't get this to work. 我认为indexOf会被使用,但不能让它工作。

Any help would be really appreciated. 任何帮助将非常感激。

var zoomthumbsarray = [] // Empty array which gets populated by .push below during loop

for (var i = 0; i < storeImgsArr.length; i++) { // storeImgsArr finds the quantity of attributes present against the product. This loops and increments counter if there is another attibute image
    for (var e = 0; e < storeImgsArr[i].images.imgL.length; e++) { // Loop and increment counter if there is a Large image 

        zoomthumbsarray.push(storeImgsArr[i].images.imgS[e].slice(-16)); // Slices off last 16 characters of image path i.e. _navy_xsmall.jpg or 46983_xsalt1.jpg and pushes this into 'zoomthumbsarray' array

        // if statement sits here to build the html to add the image to the slider

    }
}

zoomthumbsarray = [] // Resets array to zero

ANSWER As answered by Chris I used $.unique to only keep unique values in the array. 答案正如克里斯所回答的,我使用$ .unique只保留数组中的唯一值。 Then wrap an if statement around the code to build the thumb image html if the array === 0 or if the current image isn't already in the array. 然后在代码周围包装if语句,以构建拇指图像html,如果数组=== 0或者当前图像尚未在数组中。

Updated code below: 更新的代码如下:

var zoomthumbsarray = [] // Empty array which gets populated by .push below during loop

for (var i = 0; i < storeImgsArr.length; i++) { // storeImgsArr finds the quantity of attributes present against the product. This loops and increments counter if there is another attibute image

    if (zoomthumbsarray === 0 || zoomthumbsarray.indexOf(storeImgsArr[i].images.imgS[e].slice(-16)) < 0) { // If statement is true if array === 0 or if the current image isn't already in the array 

        for (var e = 0; e < storeImgsArr[i].images.imgL.length; e++) { // Loop and increment counter if there is a Large image 

            zoomthumbsarray.push(storeImgsArr[i].images.imgS[e].slice(-16)); // Slices off last 16 characters of image path i.e. _navy_xsmall.jpg or 46983_xsalt1.jpg and pushes this into 'zoomthumbsarray' array

            zoomthumbsarray = $.unique(zoomthumbsarray); //Keeps only unique elements

            // if statement sits here to build the html to add the image to the slider

        }
    }
}

zoomthumbsarray = [] // Resets array to zero zoomthumbsarray = [] //将数组重置为零

Some cheap and dirty ideas: 一些廉价而肮脏的想法:

Using underscore / lodash : 使用下划线 / lodash

zoomthumbsarray = _.uniq(zoomthumbsarray); //Keeps only unique elements

jQuery has one as well: jQuery 还有一个

zoomthumbsarray = $.unique(zoomthumbsarray); //Keeps only unique elements

then you loop through the array and build HTML. 然后循环遍历数组并构建HTML。

Update : There's something a bit odd about the rest of the JS. 更新 :JS的其余部分有些奇怪。 Might this work (if you're using a new enough browser)? 可能这项工作(如果你使用的是足够新的浏览器)?

var zoomthumbsarray = [];
storeImgsArr
    .map(function(item) { return item.images.imgS; })
    .forEach(function(imgS) {
        zoomthumbsarray = zoomthumbsarray.concat(imgS.map(function(imagePath) {
            return imagePath.slice(-16);
        }));
    });
zoomthumbsarray = $.unique(zoomthumbsarray);

instead of using an array you can use an object to store the images as keys and a dummy value (possibly true). 您可以使用对象将图像存储为键和虚拟值(可能为true),而不是使用数组。 then you can extract the keys from this object. 然后你可以从这个对象中提取密钥。

var images = {};

for (var i = 0; i < storeImgsArr.length; i++) { 
    for (var e = 0; e < storeImgsArr[i].images.imgL.length; e++) { 
        images[storeImgsArr[i].images.imgS[e].slice(-16))] = true; 
    }
}
var zoomthumbsarray = [];
for(var k in images) {
    zoomthumbsarray.push(k);
    // build the html to add the image to the slider
}

EDIT: Added build html comment 编辑:添加了构建HTML评论

I have tried indexOf (see first if statement below) but this doesn't work. 我尝试过indexOf(参见下面的第一条if语句),但这不起作用。

As @elclanrs said, indexOf does return the index in the array not a boolean. 正如@elclanrs所说, indexOf确实返回数组中的索引而不是布尔值。 You only will need to see if it's >= 0 to test whether an image is already contained in the array. 您只需要查看它是否>= 0来测试图像是否已包含在数组中。

var zoomthumbsarray = [];
for (var i = 0; i < storeImgsArr.length; i++) {
    for (var e = 0; e < storeImgsArr[i].images.imgL.length; e++) {
        var image = storeImgsArr[i].images.imgS[e].slice(-16);

        if (zoomthumbsarray.indexOf(image) < 0) { // not yet in the array
            zoomthumbsarray.push();
            // and build the html to add the image to the slider
        }
    }
}

If you have really lots of images and notice this starts slowing the page down, then there are too many images in your page anyway. 如果你有很多图像,并注意到这开始减慢页面的速度,那么你的页面中的图像太多了。 No, joke aside; 不,开个玩笑; …then check the optimisation by @Ivey. ...然后通过@Ivey检查优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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