Nidhin Kumar
Nidhin Kumar

Reputation: 3579

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

Upvotes: 1

Views: 1046

Answers (2)

tevemadar
tevemadar

Reputation: 13195

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

Upvotes: 0

kennyvh
kennyvh

Reputation: 2854

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

Upvotes: 1

Related Questions