skip to Main Content

I have a basic array of objects that I want to reorder based on the value of another array.

This is something similar to what I want to do but is based purely on an array

items = [
    ['Anne', 'a'],
    ['Bob', 'b'],
    ['Henry', 'b'],
    ['Andrew', 'd'],
    ['Jason', 'c'],
    ['Thomas', 'b']
]

/* This works fine */
sorting = [ 'b', 'c', 'b', 'b', 'c', 'd' ];
result = [];

sorting.forEach(function(key) {
    var found = false;
    items = items.filter(function(item) {
        if(!found && item[1] == key) {
            result.push(item);
            found = true;
            return false;
        } else
            return true;
    })
})

result.forEach(function(item) {
    console.log(item[0]) /// Bob Jason Henry Thomas Andrew
})

The code I have is:

driverArray = [
   {label: "Driver 1",  pos: 1},
   {label: "Driver 2",  pos: 2},
   {label: "Driver 3",  pos: 3},
   {label: "Driver 4",  pos: 4},
   {label: "Driver 5",  pos: 5},
   {label: "Driver 6",  pos: 6},
   {label: "Driver 7",  pos: 7},
   {label: "Driver 8",  pos: 8},
   {label: "Driver 9",  pos: 9},
   {label: "Driver 10", pos:10}
];

newIndexes = [1,2,3,7,4,8,5,9,6,10); // These match the pos key;

I then want to sort the driverArray in the order of the newIndexes array. The newIndexes array match the Object Key ‘pos’;

2

Answers


  1. You could group by pos and then map by pos as well.

    const
        driverArray = [{ label: "Driver 1", pos: 1 }, { label: "Driver 2", pos: 2 }, { label: "Driver 3", pos: 3 }, { label: "Driver 4", pos: 4 }, { label: "Driver 5", pos: 5 }, { label: "Driver 6", pos: 6 }, { label: "Driver 7", pos: 7 }, { label: "Driver 8", pos: 8 }, { label: "Driver 9", pos: 9 }, { label: "Driver 10", pos: 10 }],
        newIndexes = [1, 2, 3, 7, 4, 8, 5, 9, 6, 10],
        temp = Object.groupBy(driverArray, ({ pos }) => pos),
        result = newIndexes.map(pos => temp[pos].shift());
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  2. Basicaly, for each position you need to find the matching item. So first we make a dictionary object from each pos to its item. It would be more efficient.

    const driverArray = [
       {label: "Driver 1",  pos: 1},
       {label: "Driver 2",  pos: 2},
       {label: "Driver 3",  pos: 3},
       {label: "Driver 4",  pos: 4},
       {label: "Driver 5",  pos: 5},
       {label: "Driver 6",  pos: 6},
       {label: "Driver 7",  pos: 7},
       {label: "Driver 8",  pos: 8},
       {label: "Driver 9",  pos: 9},
       {label: "Driver 10", pos:10}
    ];
    
    const newIndexes = [1,2,3,7,4,8,5,9,6,10]; 
    
    // first to make efficient perpare dictionary
    const dict = driverArray.reduce(function(agg, item) {
      agg[""+item.pos] = item;
      return agg
    }, {})
    
    // now let's create sorted array
    const result = newIndexes.map(function(pos) {
      return dict[""+pos]
    })
    
    console.log(result)
    .as-console-wrapper { min-height: 100%; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search