const arrA = ['john','matha','lisa','adam']; const arrB = ['tom','andrew','lisa','adam']; console.log(arrA.filter(name => !arrB.includes(name))); // ["john", "matha"] console.log(arrA.filter(name => !arrB.some(other => other === name))); // ["john", "matha"] console.log(arrA.filter(name => arrB.every(other => other !== name))); // ["john", "matha"]
JavaScript data types and data structures
Strings are a primitive type. Objects are a structural type.
So with objects you simply have to "dig deeper" until you can compare the primitive types (referenced by the object) that matter to you.
const formA = [ { id: 37, name: 'New', another: 'things' }, { id: 31, name: 'Old', another: 'things' }, { id: 3, name: 'Oldest', another: 'things' }, ]; const formB = [ { id: 37, name: 'New', another: 'things' }, { id: 31, name: 'Old', another: 'things' }, ]; console.log( formA.filter((itemA) => formB.every((itemB) => itemB.name !== itemA.name)) ); // [ {id: 3, name: "Oldest", another: "things" } ] function differenceByName(itemsA, itemsB) { const keepNotInB = (itemA) => { const isNameDifferent = (itemB) => itemB.name !== itemA.name; return itemsB.every(isNameDifferent); }; return itemsA.filter(keepNotInB); } console.log(differenceByName(formA, formB)); // [ {id: 3, name: "Oldest", another: "things" } ]
Array.prototype.every()
thank you peerreynders, i like this approach, quiet understandable too, thanks alot for the efforts i am more than ever energised :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript data types and data structures
Strings are a primitive type.
Objects are a structural type.
So with objects you simply have to "dig deeper" until you can compare the primitive types (referenced by the object) that matter to you.
Array.prototype.every()
thank you peerreynders, i like this approach, quiet understandable too, thanks alot for the efforts i am more than ever energised :)