jQuery - Finding Distinct Values in Object Array

后端 未结 1 1366
醉话见心
醉话见心 2021-02-06 15:40

I\'ve got an array of objects where each object has fields like title, description, family, etc. How can I perform a jQuery operation that grabs all objects in this array with

相关标签:
1条回答
  • 2021-02-06 16:40

    You could do:

    var array = [{
        familyName: "one"},
    {
        familyName: "two"},
    {
        familyName: "one"},
    {
        familyName: "two"}];
    
    var dupes = {};
    var singles = [];
    
    $.each(array, function(i, el) {
    
        if (!dupes[el.familyName]) {
            dupes[el.familyName] = true;
            singles.push(el);
        }
    });
    

    Singles is an array with only DISTINCT objects

    EDIT - i have blogged about this and given a more elaborate answer http://newcodeandroll.blogspot.it/2012/01/how-to-find-duplicates-in-array-in.html

    0 讨论(0)
提交回复
热议问题