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
  • 90,923
  • 26
  • 142
  • 180
Toniq
  • 4,492
  • 12
  • 50
  • 109

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,709
  • 1
  • 17
  • 18
  • 1
    Great answer, this works also: b.map(function (item, i, _a) { return _a[ a[ i ] ] }) – KQI Jan 04 '16 at 18:38