I'm trying to create a new group of objects adding a new component to the first one from the second one, based on a condition. Here is an example:
Have this 2 group of objects:
const one = [{code: 'a'},{ code: 'b'}, {code: 'c'}]
const two = [{code: 'b', status: 'active'}]
and would like to have a result like:
{ code: 'a', status: 'empty' }
{ code: 'b', status: 'active' }
{ code: 'c', status: 'empty' }
This is the code I wrote as example:
const one = [{code: 'a'},{ code: 'b'}, {code: 'c'}]
const two = [{code: 'b', status: 'active'}]
var final = []
var temp = {}
for(let holiday of one) {
// here I started to create a temp object with components of object one
temp.code = holiday.code
// here I check if same code from object one exist in any object two
var checksub= two.find(o => o.code == holiday.code)
// then assigns proper value to temp object from object two, of a default instead
if(checksub){temp.status = checksub.status}
else {temp.status = 'empty'}
// then push the object into new array of objects
final.push(temp)
}
console.log(final)
But the result I'm getting is:
[
{ code: 'c', status: 'empty' },
{ code: 'c', status: 'empty' },
{ code: 'c', status: 'empty' }
]
Why I'm getting 3 times the last object? I've tried different methods (using forEach and for loops) and always get same weird result.... what am I doing wrong? Thanks!!!