admin管理员组

文章数量:1391989

I have a json object:

var object1 = [
                {"value1": "1", "value2": "2", "value3": "3",},
                {"value1": "1", "value2": "5", "value3": "7",},
                {"value1": "6", "value2": "9", "value3": "5",},
                {"value1": "6", "value2": "9", "value3": "5",}
]

Now I want to

  • take each record out of that object
  • and check how many times exact copy of that record is appearing in that object?

If it is only 1 copy do something and if it is more than 2 do something else. There are few answers on JSON duplicates but they target specific value not full record.

So I will take the record:

{ "value1": "1", "value2": "2", "value3": "3",}

and pare it against object1. The above record will return 1 as there is only 1 copy inside object1

For Future use. Given these records

var asset = [
    { value1: "1", value2: "2", value3: "3" },
    { value1: "1", value2: "5", value3: "7" },
    { value1: "6", value2: "9", value3: "5" },
    { value1: "6", value2: "9", value3: "5" }
];

This code can be used to find duplicates:


function countEqual(oo, pp) {
    var count = 0;
    oo.forEach(function (el) {
        var i, equal = true;
        for (i in el) {
            equal = equal && el[i] === pp[i];
        }
        equal && count++;
    });
    return count;
}

var cleaned = [];

asset.forEach(function (itm) {
    var unique = true;
    cleaned.forEach(function (itm2) {
        if (_.isEqual(itm, itm2)) unique = false;
    });
    if (unique) cleaned.push(itm);
});

for (var i = 0; i < cleaned.length; i++) {
    if (countEqual(asset, cleaned[i]) === 1) {
        // DO SOMETHING
    }
    else {
        // DO SOMETHING ELSE
    }
}

I have a json object:

var object1 = [
                {"value1": "1", "value2": "2", "value3": "3",},
                {"value1": "1", "value2": "5", "value3": "7",},
                {"value1": "6", "value2": "9", "value3": "5",},
                {"value1": "6", "value2": "9", "value3": "5",}
]

Now I want to

  • take each record out of that object
  • and check how many times exact copy of that record is appearing in that object?

If it is only 1 copy do something and if it is more than 2 do something else. There are few answers on JSON duplicates but they target specific value not full record.

So I will take the record:

{ "value1": "1", "value2": "2", "value3": "3",}

and pare it against object1. The above record will return 1 as there is only 1 copy inside object1

For Future use. Given these records

var asset = [
    { value1: "1", value2: "2", value3: "3" },
    { value1: "1", value2: "5", value3: "7" },
    { value1: "6", value2: "9", value3: "5" },
    { value1: "6", value2: "9", value3: "5" }
];

This code can be used to find duplicates:


function countEqual(oo, pp) {
    var count = 0;
    oo.forEach(function (el) {
        var i, equal = true;
        for (i in el) {
            equal = equal && el[i] === pp[i];
        }
        equal && count++;
    });
    return count;
}

var cleaned = [];

asset.forEach(function (itm) {
    var unique = true;
    cleaned.forEach(function (itm2) {
        if (_.isEqual(itm, itm2)) unique = false;
    });
    if (unique) cleaned.push(itm);
});

for (var i = 0; i < cleaned.length; i++) {
    if (countEqual(asset, cleaned[i]) === 1) {
        // DO SOMETHING
    }
    else {
        // DO SOMETHING ELSE
    }
}
Share Improve this question edited Mar 3, 2023 at 9:41 surfmuggle 5,9748 gold badges56 silver badges92 bronze badges asked Jun 22, 2015 at 14:12 Danielok1993Danielok1993 3591 gold badge6 silver badges15 bronze badges 6
  • What do you consider as duplicate? – Praveen Kumar Purushothaman Commented Jun 22, 2015 at 14:14
  • 4 1) That's not JSON. 2) you can't have duplicate keys in JavaScript objects. The end result of your assignment is that each object in object1 will have one value2 key. – Andrew Whitaker Commented Jun 22, 2015 at 14:14
  • Sorry yeah I wrote bad example I removed the keys – Danielok1993 Commented Jun 22, 2015 at 14:16
  • Also, that's not an object, it's an array. – Andy Commented Jun 22, 2015 at 14:18
  • 1 possible duplicate of Remove duplicates from an array of objects in javascript – Patrick Evans Commented Jun 22, 2015 at 14:18
 |  Show 1 more ment

2 Answers 2

Reset to default 1
var asset = [
    { value1: "1", value2: "2", value3: "3" },
    { value1: "1", value2: "5", value3: "7" },
    { value1: "6", value2: "9", value3: "5" },
    { value1: "6", value2: "9", value3: "5" }
];

function countEqual(oo, pp) {
    var count = 0;
    oo.forEach(function (el) {
        var i, equal = true;
        for (i in el) {
            equal = equal && el[i] === pp[i];
        }
        equal && count++;
    });
    return count;
}
console.log(countEqual(asset, { value1: "1", value2: "1", value3: "2" })); // 0
console.log(countEqual(asset, { value1: "1", value2: "2", value3: "3" })); // 1
console.log(countEqual(asset, { value1: "6", value2: "9", value3: "5" })); // 2

If what you are looking for is a function that will count the number of occurrences of an object in an array, the following code should help you (this assumes that the properties in your objects will always follow the same order. If you aren't sure about that you should use a proper equality check function):

var nbOcc = function (needle, haystack) {
  return haystack.filter(function (record) {
    return JSON.stringify(needle) === JSON.stringify(record);
  }).length;
};

console.log(nbOcc({
  "value1": "1",
  "value2": "2",
  "value3": "3",
}, object1)); // 1

console.log(nbOcc({
  "value1": "6",
  "value2": "9",
  "value3": "5",
}, object1)); // 2

JSFiddle link

本文标签: javascriptCheck for duplicates in JSONStack Overflow