Home > OS >  Rearrange array with new order using javascript
Rearrange array with new order using javascript

Time:10-18

I have an array

const arr = [
   {label: 'a', width: 200},
   {label: 'b', width: 200},
   {label: 'c', width: 200},
   {label: 'd', width: 200},
   {label: 'e', width: 200}
];

given another array

const data = ['d', 'e', 'a', 'c', 'b'];

It need's to re-arrange first array based on the new data.

What function should I use in javascript?

Edit: Thank you for very interesting comments. But to make it more complicated, let's assume that data could also be not the full list of the first array.

const data = ['a', 'c'];

and it still should outcome the first array where first two elements are a & c, and remaining ones are b, d, e. Finished array should be in a list of a, c, b, d, e.

CodePudding user response:

const arr = [
  { label: "a", width: 200 },
  { label: "b", width: 200 },
  { label: "c", width: 200 },
  { label: "d", width: 200 },
  { label: "e", width: 200 }
];

const data = ["d", "e", "a", "c", "b"];


const updatedArray = data.map((item) => arr.find((t) => t.label === item));
console.log(updatedArray);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

simply :

const arr = 
  [ { label: 'a', width: 200} 
  , { label: 'b', width: 200} 
  , { label: 'c', width: 200} 
  , { label: 'd', width: 200} 
  , { label: 'e', width: 200} 
  ] 
const data = ['d', 'e', 'a', 'c', 'b']

arr.sort((a, b) => data.indexOf(a.label) - data.indexOf(b.label))

console.log(arr)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

To solve that, use map() and find() methods like that:

const sorted = data.map(element =>  arr.find(obj => obj.label === element))

CodePudding user response:

  • Using Array#reduce, iterate over data to save element-index in a Map
  • Using Array#sort, sort arr by the value of each element in the above Map

const sort = (arr = [], data = []) => {
  const indicesMap = data.reduce((map, e, i) => map.set(e, i), new Map);
  return [...arr].sort(({ label: a}, { label: b }) => {
    const indexA = indicesMap.get(a), indexB = indicesMap.get(b);
    return (indexA === undefined || indexB === undefined) 
      ? isNaN(indexA) - isNaN(indexB)
      : indexA - indexB;
  });
}

const arr = [ {label: 'a', width: 200}, {label: 'b', width: 200}, {label: 'c', width: 200}, {label: 'd', width: 200}, {label: 'e', width: 200} ];
console.log( sort(arr, ['d', 'e']) );
console.log( sort(arr, ['a', 'd']) );
console.log( sort(arr, ['d', 'e', 'a', 'c', 'b']) );
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

here's a short solution using sort

const arr = [
   {label: 'a', width: 200},
   {label: 'b', width: 200},
   {label: 'c', width: 200},
   {label: 'd', width: 200},
   {label: 'e', width: 200}
];
const data = ['d', 'e', 'a', 'c', 'b'];

const sorted = arr.sort(function(a, b){  
  return data.indexOf(a.label) - data.indexOf(b.label);
});

console.log(sorted)
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can take the order of the sorted array as weightage and sort the array.

const arr = [
   {label: 'a', width: 200},
   {label: 'b', width: 200},
   {label: 'c', width: 200},
   {label: 'd', width: 200},
   {label: 'e', width: 200}
];

const sortOrder = ['d', 'e', 'a', 'c', 'b'];


arr.sort((x, y) => sortOrder.indexOf(x.label) - sortOrder.indexOf(y.label))

console.log(arr)
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related