I have 2 arrangements of objects
The first one called data :
const data = [
{
id: 1,
nombre: 'Piero',
},
{
id: 4,
nombre: 'Nelson',
},
{
id: 7,
nombre: 'Diego'
},
]
and the second one called subs :
const subs = [
{
id: 1,
name: 'Temprano',
},
{
id: 4,
name: 'A tiempo',
},
{
id: 7,
name: 'Tarde'
},
]
In which I want to compare that if they have the same id the subs arrangement will pass your name and if it does not match that you put a '-'
to the array > data try this way:
data.forEach((d)=>{
subs.forEach((s)=>{
if(d.id === s.id){
d.subname = s.name;
}
else {
d.subname = '-';
}
});
});
But I always print the values with '-'
as if it did not match any. What part am I doing wrong? Is there another, simpler way to do this? I would greatly appreciate your help.
The size of the subs fix may vary