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

javascript - Removing Duplicate object from array in jquery code not working - Stack Overflow

programmeradmin5浏览0评论

This is my array in jquery , which contains duplicate objects/elements :

[{
    "name": "hello",
    "label": "world"
}, {
    "name": "abc",
    "label": "xyz"
}, {
    "name": "hello",
    "label": "world"
}]

I am using the following piece of code to remove duplicate elements but it not working the duplicate elements are not removed.

var result = [];

$.each(subservices, function (i, e) {
    if ($.inArray(e, result) == -1)
        result.push(e);
});

alert(JSON.stringify(result));

This is my array in jquery , which contains duplicate objects/elements :

[{
    "name": "hello",
    "label": "world"
}, {
    "name": "abc",
    "label": "xyz"
}, {
    "name": "hello",
    "label": "world"
}]

I am using the following piece of code to remove duplicate elements but it not working the duplicate elements are not removed.

var result = [];

$.each(subservices, function (i, e) {
    if ($.inArray(e, result) == -1)
        result.push(e);
});

alert(JSON.stringify(result));
Share Improve this question edited Oct 10, 2015 at 8:08 Tushar 87.2k21 gold badges163 silver badges181 bronze badges asked Oct 10, 2015 at 8:06 Shivkumar MallesappaShivkumar Mallesappa 2713 gold badges7 silver badges19 bronze badges 1
  • hi there please add jsfiddle – Sina R. Commented Oct 10, 2015 at 8:14
Add a ment  | 

3 Answers 3

Reset to default 9

Function $.inArray works fine for simple types (e.g. number or string), but for plex types it does not produce the correct result, because it tries to match by reference. Instead of using inArray in your loop you can search the array using function grep:

var subservices = [{
        "name": "hello",
        "label": "world"
    }, {
        "name": "abc",
        "label": "xyz"
    }, {
        "name": "hello",
        "label": "world"
    }
];

var result = [];
$.each(subservices, function (i, e) {
    var matchingItems = $.grep(result, function (item) {
       return item.name === e.name && item.label === e.label;
    });
    if (matchingItems.length === 0){
        result.push(e);
    }
});

//displays result [{"name":"hello","label":"world"},{"name":"abc","label":"xyz"}]
alert(JSON.stringify(result));

Here is a working jsFiddle

You need to filter array by unique name/value. Here is some pure JS solution:

var data = [{
    "name": "hello",
    "label": "world"
}, {
    "name": "abc",
    "label": "xyz"
}, {
    "name": "hello",
    "label": "world"
}];

var result = data.filter(function(el, i, x) {
    return x.some(function(obj, j) {
        return obj.name === el.name && (x = j);
    }) && i == x;
});

alert(JSON.stringify(result, null, 4));

This is because these two objects are distinct, even though all the attributes inside are the same. You can see this from:

console.log(result[0] === result[2]);

which results in false.

Instead, you need to iterate through your array based on a unique identifier, such as name & label as such:

for(var i = 0, i < results.length; i++) {
    if (result[i].name === ... && result[i].label === ...) {
        index = i;
        break;
    }
}

to check if your item is unique.

发布评论

评论列表(0)

  1. 暂无评论