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

0

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, this works also: b.map(function (item, i, _a) { return _a[ a[ i ] ] })

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.