0

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 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
    }
}
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Danielok1993
  • 359
  • 1
  • 6
  • 14

2 Answers2

1

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

Thomas Eschemann
  • 995
  • 7
  • 18
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
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Yes that worked fine. From my side I had to clean up "asset" object to contain only unique values. Then, use for loop on the new unique array and apply it to your code. Thank you! – Danielok1993 Jun 22 '15 at 15:08