I have 2 objects
const obj1 = {
item1: {
name: '1'
},
item2: {
name: '2'
}
}
const obj2 = {
item1: {
sample: 'sample1'
},
item2: {
sample: 'sample2'
}
}
How can I loop over the second object obj2
, find the first match against object obj1
and return it?
In above example, I should return
{
item1: {
name: '1'
}
}
since item1
is the first match between the 2 objects and I want what’s inside obj1.
Tried the following:
const keys = obj2 && Object.keys(obj2);
const output = () => {
if (keys) {
const finalResponse = keys.map(key => {
if (obj1[key]) return obj1[key];
return undefined;
});
}
}
But ends up undefined even though there is a match on item1.
What am I doing wrong and is there a cleaner way to do this.
Fine to loop over obj1 or obj2 as long as I can return the match in obj1.