I am trying to get the values from an array and assign to an array of objects as a new property.
The array of objects:
const employees = [
{
firstName: "Ana",
lastName: "Rosy"
},
{
firstName: "Zion",
lastName: "Albert"
},
{
firstName: "John",
lastName: "Doe"
}
];
An array
const ages = [30, 60, 45]
Desired result
const employees = [
{
firstName: "Ana",
lastName: "Rosy",
age:30
},
{
firstName: "Zion",
lastName: "Albert",
age:60
},
{
firstName: "John",
lastName: "Doe",
age:45
}
];
I tried something like this
const employeesWithAge = (ages) => {
return employees.map((row) => {
return {
...row,
age: ages
};
});
};
const result = employeesWithAge(ages);
console.log("result", result);
But it adds the entire array of ages to the employees array of objects.
Any help will be appreciated