最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Check whether an object with property value 'x' exists in an array in Javascript - Stack Overflow

programmeradmin0浏览0评论

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.

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

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: ''};
var obj2 = {IMAGEURL: ''};
var obj3 = {IMAGEURL: ''};

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?

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.

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

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?

Share Improve this question asked Aug 17, 2013 at 4:23 JVGJVG 21.2k48 gold badges140 silver badges215 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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]);
}

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

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

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

发布评论

评论列表(0)

  1. 暂无评论