I am trying to compare values1
and values2
by their property and the values. If the value of the name
property matches up with each other, I want to push the property and value to value3.
But I am getting this error: Cannot read property 'age' of undefined
const values1 = [
{ name: 'dog', surname: 'good', skills: 'programming' },
{ name: 'cat', surname: 'soft', skills: 'engineer' },
{ name: 'elephant', surname: 'big', skills: 'programming' }
]
const values2 = [
{ name: 'cat', age: '12' },
{ name: 'elephant', age: '13' },
]
const values3 = values1.map((value1) => {
return Object.assign(value1, { age: values2.filter(value2 => value2.name === value1.name)[0].age })
})
console.log(values3)
This is the result I would like to return.
{
name: 'cat',
surname: 'soft',
skills: 'engineer'
age: '12'
},
{
name: 'dog',
surname: 'good',
skills: 'programming',
},
{
name: 'elephant',
surname: 'big',
skills: 'programming'
age: '23'
}