JSFiddle - React, Tailwind, and code Playground
JavaScript
console.clear();
// in my case b will ALWAYS be a subset of a, but may lack elements that exist in a
// please correct me if I used `subset` incorrectly
var a = [{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}],
b = [{id: 1}, {id: 2}, {id: 3}, {id: 5}],
result = [],
aLength = a.length,
bLength = b.length;
// I want result to contain only {id:4} - the one element in a but not in b
console.log(aLength);
for (var i = 0; i < aLength; i++) {
var inB = checkInB(a[i]);
console.log("inB: " , inB, "a[i]: ", a[i]);
if (! inB) { result.push(a[i]) };
}
function checkInB(obj) {
for (var i = 0 ; i < bLength; i++) {
if (obj.id === b[i].id) {
console.log("obj ", obj.id, "b", b[i].id);
return true;
}
}
return false;
}
console.log("final result array", result);