Create a new array using the elements of one array as an index to select elements in another array - JavaScript

It took almost a day and a half, and I still don't know why this works. If there is a better way (s), I would love to hear that. In the current 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 that produces the code:

[["One", "Four", "Nine"], ["B", "E", "J"]]
+4
source share
3 answers

You can use and map() 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>');
Run codeHide result

Update: In case you need to get the values ​​in the same order as the index array, 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>');
Run codeHide result
+6
source

Array.prototype.map valuesArray 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>');
Hide result
0

:

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) ) );
Hide result
0

Source: https://habr.com/ru/post/1611822/


All Articles