[英]Select elements from a Javascript Array using one of the elements as a reference point
[英]Create new array using elements of one array as an index to select elements in a different array - JavaScript
花了差不多一天半的时间来完成这个,我仍然不确定它为什么会起作用。 如果有更好的方法可以实现,我很乐意听到它。 在目前的状态,我希望这有助于某人。
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);
这是我正在寻找上面代码产生的结果:
[["One", "Four", "Nine"], ["B", "E", "J"]]
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>');
更新:如果您需要以索引数组的相同顺序获取值,则使用
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>');
您可以考虑使用数组方法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>');
一个解决方案
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.