JSFiddle - React, Tailwind, and code Playground
JavaScript
function compare(n, o) {
counts = {};
// count occurrencies of each `id` in new_array
for (var i = 0; i < n.length; i++) {
var key = n[i].id;
counts[key] = (counts[key] || 0) + 1;
}
console.log(JSON.stringify(counts, null, 2)); // stringify because it will get modified afterwards, and the log would show the updated object
// takes each item ID in array_old
return o.every(function (item) {
var key = item.id;
// and checks whether the count is positive
if (key in counts && counts[key] > 0) {
console.log(key + " existed in new_array");
counts[key]--; // decrement counter for each old occurrence
return true; // go on searching
} else {
alert(key + " was counted in `new_array` at least once too less!");
return false;
}
});
};
alert("result: " + compare(
[{id: "5436", title: "I Like you boy"}, {id: "1132", title: "I'm fine"}],
[{id: "5436", title: "I Like you boy"}, {id: "5437", title: "Hello how are you"}]
));