I have two array of objects like below.I would like to update the first array based on the values of the second array of objects. i.e. If both the array has same value then update the first array object with the new value else update it with empty string;
Array1:
shuffledColumns: [
{ key: 'Hardware ID', field: 'hardware_id', hide: false, sort: 'asc' },
{ key: 'Maximum Flows', field: 'max_flows', hide: false, sort: 'desc' },
{ key: 'Name', field: 'name', hide: false, sort: 'desc' },
{ key: 'Availability', field: 'availability', hide: false, sort: 'desc' },
{ key: 'Description', field: 'description', hide: false, sort: 'desc' },
{ key: 'Last updated', field: 'updated_at', hide: false, sort: 'desc' },
],
Array 2:
const sorted = [
{colId: "max_flows", sort: "desc"},
{colId: "name", sort: "asc"},
{colId: "availability", sort: "asc"},
];
Expected Output:
shuffledColumns: [
{ key: 'Hardware ID', field: 'hardware_id', hide: false, sort: '' },
{ key: 'Maximum Flows', field: 'max_flows', hide: false, sort: 'desc' },
{ key: 'Name', field: 'name', hide: false, sort: 'asc' },
{ key: 'Availability', field: 'availability', hide: false, sort: 'asc' },
{ key: 'Description', field: 'description', hide: false, sort: '' },
{ key: 'Last updated', field: 'updated_at', hide: false, sort: '' },
],
The mathced column should update with the new value and the rest of the columns as empty
I have tried the below approach but once the first item in the array gets completed the second item replaces the old one.
sorted.forEach(column => {
shuffledColumns.forEach(data => {
if (data.field === column.colId) {
data.sort = column.sort;
} else {
data.sort = '';
}
});
});