0

I have an array or numbers and objects (same length):

var a = [2,0,1], b = [obj1,obj2,obj3];

I would like to reposition items in array 'b' to the position of numbers in array 'a'.

Could be done with jquery as well.

How can I do that most easily?

Thanks

Blazemonger
  • 86,267
  • 25
  • 136
  • 177
Toniq
  • 3,918
  • 9
  • 42
  • 96

1 Answers1

2

To do this there is no an auto function but you can use array map to get this result.

var orderByArray = function(order, data) {
  return order.map(function(pos) {
    return data[pos];
  });
};

var a = [2,0,1];
var b = ['obj1', 'obj2', 'obj3'];

var result = orderByArray(a, b);

console.log('result', result);
Jose Mato
  • 2,643
  • 1
  • 16
  • 17
  • 1
    Great answer, this works also: b.map(function (item, i, _a) { return _a[ a[ i ] ] }) – KQI Jan 04 '16 at 18:38