[英]Update the first array object value based on the second array by comparing a field
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.我有两个对象数组,如下所示。我想根据第二个对象数组的值更新第一个数组。 ie If both the array has same value then update the first array object with the new value else update it with empty string;即如果两个数组具有相同的值,则用新值更新第一个数组对象,否则用空字符串更新它;
Array1:数组1:
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:数组 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 mathced 列应使用新值更新,其余列应为空
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 = '';
}
});
});
This solution does what you want:此解决方案可以满足您的需求:
shuffledColumns.forEach(col => {
// if the field exists in the second array, we will change this to true
let fieldMatch = false;
// go through the second array and find the matching colId if it exists
sorted.forEach(data => {
if (col.field === data.colId) {
fieldMatch = true;
col.sort = data.sort;
}
})
// if it doesn't exist, set sort = ''
if (!fieldMatch) col.sort = '';
})
You could put the elements of sorted
into a Map
, and then you can directly use get()
to see if an element should / should not have a sort
, and what that should be:您可以将sorted
的元素放入Map
,然后您可以直接使用get()
来查看元素是否应该/不应该有sort
,以及应该是什么:
let 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' }, ]; const sorted = [ {colId: "max_flows", sort: "desc"}, {colId: "name", sort: "asc"}, {colId: "availability", sort: "asc"}, ]; function update(columns,sorted) { let helper=new Map(); for(const item of sorted) helper.set(item.colId,item); for(const item of columns) { let s=helper.get(item.field); item.sort=s?s.sort:""; } } update(shuffledColumns,sorted); console.log(shuffledColumns);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.