简体   繁体   English

通过比较字段更新基于第二个数组的第一个数组对象值

[英]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.

相关问题 通过比较属性将第二个数组与第一个数组合并,如果对象不是第二个数组的一部分,则将属性添加到第一个数组中的对象 - merge second array with first array by comparing properties and add properties to objects in first array if object is not part of second array 比较两个数组并匹配第二个数组对象中第一个数组对象的一个键,如果找到则插入一个键和值 - comparing two arrays and match one key from first array's object in the second array's object if found then insert a key and value 根据对象的值更新数组 - Update an array based on the value of object 如何使用angular6在基于id值的第一个数组对象中添加第二个数组对象? - How to add second array objects in first array object based on id value using angular6? 基于第一个值作为键和第二个值作为javascript中的值创建一个对象数组 - Create an array of object based on first value as key and second value as value in javascript 比较数组值和对象 - Comparing array value with object 无法将第二个数组对象值添加到 javascript 中的第一个对象数组中 - Unable to add second array object value into first array of objects in javascript 如果第二个数组包含具有除主值以外的其他值的相同对象,如何更新第一个数组的对象 - How can I update the first array's object if second array contains same object with different values except a primary value Javascript 比较两个对象数组并根据第二个数组从第一个数组中删除元素并删除第一次出现的记录 - Javascript comparing two array of objects and remove the elements from first array based on second array and remove first occurence records 如何通过比较 arrays 中的 id 将第一个数组中的值分配给第二个数组中的新键。 在 javascript - How can i assign value in first array to new key in second array by comparing id in both arrays . in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM