简体   繁体   English

使用一个数组的元素创建新数组作为索引,以选择不同数组中的元素 - JavaScript

[英]Create new array using elements of one array as an index to select elements in a different array - JavaScript

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

You can use map() and filter() 你可以使用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>'); 

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

You may consider an array method Array.prototype.map for iterating over the valuesArray and for the arrayIndex . 您可以考虑使用数组方法Array.prototype.map来迭代valuesArrayarrayIndex

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用其中一个元素作为参考点从Javascript数组中选择元素 - Select elements from a Javascript Array using one of the elements as a reference point Javascript - 使用索引替换数组中的多个元素 - Javascript - Replace multiple elements in an array using index 如何比较一个数组中的元素与Javascript中另一个数组的索引? - how to compare the elements of one array to the index of another array in Javascript? 嵌套数组 object 与另一个元素数组比较并使用 Javascript 或 ES6 创建新数组 - nested array object comparison with another array of elements and create new array using Javascript or ES6 使用javascript从数组中选择某些元素 - Select certain elements from an array using javascript Javascript-使用2 select访问数组的元素 - Javascript - Access elements of array using 2 select 通过推送数组 JavaScript 中的元素来创建一个新的对象数组 - Create a new array of object by pushing elements in the array JavaScript 从数组中的元素组合创建新数组_JavaScript - create new array from combination of elements in array _ JavaScript 比较 javascript 中的数组并创建一个新的元素数组,该数组将被删除 - Compare array in javascript and create a new array of elements which is being removed 使用数组2的元素查找数组1的索引并将其存储在数组3中 - FInd and strore index of array one using array two elements and store all index of array one in array 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM