1

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 = '';
      }
    });
  });

2 Answers 2

1

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 = '';
})
Sign up to request clarification or add additional context in comments.

Comments

0

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:

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.