4

It took almost a day and half to accomplish this, and I still am not sure why it works. If there's a better way(s) to accomplish, I'd love to hear it. In its present state, I hope this helps someone.

var newValuesArray = [];
var arrayIndex = [1, 4, 9];
var valuesArray = [["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"], 
              ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
              ];

var roots = valuesArray.map(function(num) {
    arrayIndex[num];
    return arrayIndex;
});

for (var i = 0, len = roots.length; i < len; i++) {
  newValuesArray.push(roots[i].map(function(num) {
     return valuesArray[i][num];
  }));
}

console.log(newValuesArray);

This is the result I was looking for which the code above produces:

[["One", "Four", "Nine"], ["B", "E", "J"]]
2
  • Will arrayIndex always be in ascending order? What is the expected result if arrayIndex = [4, 9, 1];? Commented Oct 15, 2015 at 16:56
  • @PaulRoub, arrayIndex can be in any order. Commented Oct 15, 2015 at 17:07

3 Answers 3

6

You can use map() and filter()

var arrayIndex = [1, 4, 9];
var valuesArray = [
  ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
  ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
];

// iterate over main array using map()
var newValuesArray = valuesArray.map(function(v) {
  // iterate and filter values in inner array using filfer()
  return v.filter(function(v1, i) {
    // check index in arrayIndex
    return arrayIndex.indexOf(i) > -1;
  });
});

document.write('<pre>'+JSON.stringify(newValuesArray,null,3)+'</pre>');

Update: In case , if you need to get values in same order of index array then use

var arrayIndex = [4, 9, 1];
var valuesArray = [
  ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
  ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
];

// iterate over main array using map()
var newValuesArray = valuesArray.map(function(v) {
  // iterate the index array
  return arrayIndex.map(function(v1) {
    // get value from inner array based on index
    return v[v1];
  });
});

document.write('<pre>'+JSON.stringify(newValuesArray,null,2)+'</pre>');

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

1 Comment

Thanks, @pranav-c-balan!
0

You may consider an array method Array.prototype.map for iterating over the valuesArray and for the arrayIndex.

var arrayIndex = [1, 4, 9],
    valuesArray = [
        ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
        ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
    ],
    newValuesArray = valuesArray.map(function (a) {
        return arrayIndex.map(function (b) {
            return a[b];
        });
    });
document.write('<pre>' + JSON.stringify(newValuesArray, 0, 4) + '</pre>');

Comments

0

One solution :

var valuesArray = [
  ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"], 
  ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"],
  ["blue" , "green" , "yellow" , "orange" , "black" , "white" , "violet" , "pink" , "purple" , "grey" ]
];

/* the function ---------------------------------------------*/
var fn = function(indexed){
  
  return indexed.reduce(function( trans , value){
  
     trans.arr.forEach(function(curArray , i){
        var curBranch = (trans.rep[i] = trans.rep[i] || []);
        curBranch.push( curArray[value]  )
     })
     return trans;
  } , {arr : valuesArray , rep : []}).rep;
 
};


/* the samples -----------------------------------------------*/

var arrayIndex = [1, 4, 9];   
document.write( JSON.stringify( fn(arrayIndex) ) );


arrayIndex = [9, 4, 1];
document.write('<br>' + JSON.stringify( fn(arrayIndex) ) );


arrayIndex = [9, 9, 3, 4, 1];
document.write('<br>' + JSON.stringify( fn(arrayIndex) ) );


arrayIndex = [99, 1, 2];
document.write('<br>' + JSON.stringify( fn(arrayIndex) ) );

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.