Danielok1993
Danielok1993

Reputation: 359

Check for duplicates in JSON

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

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 compare 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
    }
}

Upvotes: 0

Views: 15536

Answers (2)

Thomas Eschemann
Thomas Eschemann

Reputation: 995

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

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386604

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

Upvotes: 1

Related Questions