Giving an array of indexes, how to push items from one array to another depending on the indexes that are specified?

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

  • answered 2017-12-06 01:35 Arash Motamedi

    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.

  • answered 2017-12-06 01:36 Sushanth --

    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);

  • answered 2017-12-06 01:36 CRice

    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))

  • answered 2017-12-06 01:41 maerics

    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]]
    

  • answered 2017-12-06 01:42 charlietfl

    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)

  • answered 2017-12-06 02:03 PRASANNA KUMAR BISWAL

    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)