Two Js Objects, Get A Value From First Object That It Missing In The Second
I have two JS objects: I am trying to filter out a value that is present at first, but missing at second. How can I do that? Ideal return value: {name: 'Sam', group: 'test'} Tried
Solution 1:
Using Array.filter, we can return items that pass a condition. In this case, the condition is "not in the second list", which we can express with Array.some.
function getItemsOnlyInFirst(first, second) {
return first.filter(
item =>
!second.some(
compareItem =>
item.name === compareItem.name && item.group === compareItem.group
)
);
}
let first = [{ name: "Sam", group: "test" }, { name: "John", group: "it" }];
let second = [{ name: "John", group: "it" }, { name: "Tim", group: "hr" }];
getItemsOnlyInFirst(first, second); // {name: "Sam", group: "test"}
Solution 2:
The problem is you cannot compare two objects with the ==
operator:
var obj1 = {test: 1}
var obj2 = {test: 1}
console.log(obj1 == obj2) // false
An easy solution is to convert the objects into the JSON format.
This works because you can compare strings:
var obj1 = JSON.stringify({test: 1})
var obj2 = JSON.stringify({test: 1})
console.log(obj1 == obj2) // true
Solution using JSON.stringify:
var first = [{name: "Sam", group: "test"}, {name: "John", group: "it"}];
var second = [{name: "John", group: "it"} , {name: "Tim", group: "hr"}];
// Convert the objects to JSONvar second_str = second.map(JSON.stringify)
var filtered = first.filter(function(elem) {
return second_str.indexOf(JSON.stringify(elem)) == -1
})
console.log(filtered)
Solution 3:
I understand they both share same interface? For complex objects you could use lodash with eg: _.differenceBy
function, but here the case is simple (still, approach is quite generic, does not depends on keys inside objects, but they need to be in flat structure [no recursion])
let isOnlyInFirst = first.filter(el => {
return !second.some(z => {
const el_keys = Object.keys(el)
const z__keys = Object.keys(z)
let flag = truefor (const key of el_keys) {
if (!z.hasOwnProperty(key) || z[key] != el[key]) flag = false
}
if (el_keys.length === z__keys.length && flag)
returntruereturnfalse
})
})
Post a Comment for "Two Js Objects, Get A Value From First Object That It Missing In The Second"