检查Javascript中的数组中是否存在属性值为“x”的对象

时间:2021-07-17 23:46:36

I have a lot of objects that I'm trying to filter out duplicates from. When an object has a property, IMAGEURL which is present in another object, I want to ignore this object and move on.

我有很多对象,我试图从中删除重复项。当一个对象有一个属性,IMAGEURL出现在另一个对象中时,我想忽略这个对象并继续前进。

I'm using nodeJS for this so if there's a library I can use to make it easier let me know.

我正在使用nodeJS,所以如果有一个库,我可以用来让它更容易让我知道。

I've done similar implementations before with checking string values in arrays, doing something like:

在检查数组中的字符串值之前,我已经完成了类似的实现,执行如下操作:

var arr = ['foo', 'bar'];
if(arr.indexOf('foo') == -1){
   arr.push('foo')
}

But this won't work for objects, as best I can tell. What are my options here? To put it more simply:

但是这对于对象来说是行不通的,正如我所知道的那样。我有什么选择?更简单地说:

var obj1 = {IMAGEURL: 'http://whatever.com/1'};
var obj2 = {IMAGEURL: 'http://whatever.com/2'};
var obj3 = {IMAGEURL: 'http://whatever.com/1'};

var arr = [obj1, obj2, obj3];
var uniqueArr = [];

for (var i = 0; i<arr.length; i++){
    // For all the iterations of 'uniqueArr', if uniqueArr[interation].IMAGEURL == arr[i].IMAGEURL, don't add arr[i] to uniqueArr
}

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


12  

You can just use an inner loop (keeping track of whether we've seen the loop by using a seen variable -- you can actually use labels here, but I find the variable method to be easier to read):

你可以使用一个内循环(跟踪我们是否通过使用一个看到的变量看到了循环 - 你实际上可以在这里使用标签,但我发现变量方法更容易阅读):

for (var i = 0; i<arr.length; i++){
    var seen = false;
    for(var j = 0; j != uniqueArr.length; ++j) {
        if(uniqueArr[j].IMAGEURL == arr[i].IMAGEURL) seen = true;
    }
    if(!seen) uniqueArr.push(arr[i]);
}

#2


3  

Here is a concise way:

这是一个简洁的方法:

var uniqueArr = arr.filter(function(obj){
    if(obj.IMAGEURL in this) return false;
    return this[obj.IMAGEURL] = true;
}, {});

http://jsfiddle.net/rneTR/2

http://jsfiddle.net/rneTR/2

Note: this is concise, but orders of magnitude slower than Nirk's answer.

See also: http://monkeyandcrow.com/blog/why_javascripts_filter_is_slow/

另见:http://monkeyandcrow.com/blog/why_javascripts_filter_is_slow/

#1


12  

You can just use an inner loop (keeping track of whether we've seen the loop by using a seen variable -- you can actually use labels here, but I find the variable method to be easier to read):

你可以使用一个内循环(跟踪我们是否通过使用一个看到的变量看到了循环 - 你实际上可以在这里使用标签,但我发现变量方法更容易阅读):

for (var i = 0; i<arr.length; i++){
    var seen = false;
    for(var j = 0; j != uniqueArr.length; ++j) {
        if(uniqueArr[j].IMAGEURL == arr[i].IMAGEURL) seen = true;
    }
    if(!seen) uniqueArr.push(arr[i]);
}

#2


3  

Here is a concise way:

这是一个简洁的方法:

var uniqueArr = arr.filter(function(obj){
    if(obj.IMAGEURL in this) return false;
    return this[obj.IMAGEURL] = true;
}, {});

http://jsfiddle.net/rneTR/2

http://jsfiddle.net/rneTR/2

Note: this is concise, but orders of magnitude slower than Nirk's answer.

See also: http://monkeyandcrow.com/blog/why_javascripts_filter_is_slow/

另见:http://monkeyandcrow.com/blog/why_javascripts_filter_is_slow/