My goal is to update an object array by matching an id with another object containing a matching key to update the object array's value key.
Working with an object array like:
const objArr = [
{
id: 6,
name: 'Activity Attendance',
type: 'Number',
value: '2000',
nameChanged: false,
typeChanged: false,
internalName: 'activity_attendance'
},
{
id: 123,
name: 'Total Number of Interacted Consumers',
type: 'Number',
value: '400',
nameChanged: false,
typeChanged: false,
internalName: 'total_number_of_interacted_consumers'
},
{
id: 140,
name: 'Booth Location',
type: 'Select (Single Answer)',
value: '',
nameChanged: false,
typeChanged: false,
internalName: 'booth_location'
}
];
And an object like:
const obj = {
'6': '1500',
'123': '180',
'140': ''
};
Desired outcome:
const objArr = [
{
id: 6,
name: 'Activity Attendance',
type: 'Number',
value: '1500',
nameChanged: false,
typeChanged: false,
internalName: 'activity_attendance'
},
{
id: 123,
name: 'Total Number of Interacted Consumers',
type: 'Number',
value: '180',
nameChanged: false,
typeChanged: false,
internalName: 'total_number_of_interacted_consumers'
},
{
id: 140,
name: 'Booth Location',
type: 'Select (Single Answer)',
value: '',
nameChanged: false,
typeChanged: false,
internalName: 'booth_location'
}
];
I have started down the route of mapping the objArr, but am not sure of how to proceed to make the "match" within obj to get the correct value to update. My failing attempt:
objArr.map(i => i.id.includes(Object.keys(obj).map(o => Number(o))));
How can I can merge into an array of objects from an object?
objArror are you deliberately wanting to create a new array?objArr.