2

I am trying to push items from one Array to another depending on the order that is supplied. Essentially i have a 2d array with a name and a price :

var myArray = [['Apples',22],['Orange',55],['Berry',23]];

Another array with the order it should be in :

var myOrder = [0,2,1];

My resulting array would look like this :

var finalArray = [['Apples',22],['Berry',23],['Orange',55]]

My initial thought process was to loop through myArray and loop through myOrder , store the object temporary at a specified index in myOrder then push to final array. I think i am over thinking it a bit, i made several attempts but with no luck whatsoever. Any help would be greatly appreciated!

6 Answers 6

5

This is a simple map() that doesn't require anything else

var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];

let final = myOrder.map(i =>  myArray[i])

console.log(final)

Sign up to request clarification or add additional context in comments.

Comments

2

The optimal way appears to me to be:

  1. Initialize empty finalArray
  2. Loop over your myOrder array

    2.1. Push myArray[index] to finalArray

Like so:

let finalArray = [];
for(let index of myOrder) {
    finalArray.push(myArray[index]);
}

Review the for...of syntax if you're not familiar with it.

Comments

1

You can use splice to insert so long as the same number of elements are present in both the arrays.

You iterate over the myOrder array and then use splice, to which the index of the new array is the current value of the iteration and then use array present in the index position of myArray

var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];

var finalArray = [];

myOrder.forEach(function(val, index) {
    finalArray.splice(val, 0, myArray[index]);
});

console.log(finalArray);

Comments

1

Easy enough using .reduce:

var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];

function reorder(array, order) {
    return order.reduce((newArray, orderIndex) => {
        newArray.push(array[orderIndex]);
        return newArray;
    }, []);
}

console.log(reorder(myArray, myOrder))

3 Comments

Thanks for the solution works perfectly, what would be the best solution to use as another commenter had a different approach and works perfectly as well?
@GrandeurH If they both work I'd say its up to you to decide. Perhaps you find one more readable than the other, or one might be more performant (although I don't know which).
I find the other easier for myself to understand so i will go for that one but i do appreciate your answer as well
1
function reorder(arr, order) {
  return order.map(function(i) {
    return arr[i];
  });
}

var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
reorder(myArray, myOrder); // => [["Apples",22],["Berry",23],["Orange",55]]

Comments

1

One of way solving this will be

var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
var finalArray;
for (x in myOrder) {
    finalArray[x] = myArray[myOrder[x]];
}

This is a beginning level solution. Also you use libraries available for java script such as underscore.js(http://underscorejs.org/) for such operations on Array and Object.

Also you can use ECMA 6, for doing this which will reduce your line of coding. Example-

var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
let finalArray = myOrder.map(i =>  myArray[i])

This is the new way of coding in javascript. In my point of view, it will be easy if you learn latest version of Java script(ECMAscript 6)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.