我正在尝试将嵌套数组中的数据选择返回到新的嵌套数组中,但是数据只是被推入了数组中。
var selection = [0,1,3,4];
var allProductData = [['Item1Sku','Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'], ['Item2Sku','Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'], ['Item3Sku','Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']]
var selectedProductData = []
for(var apd=0; apd<allProductData.length; apd++) {
for(var spd=0; spd<allProductData[apd].length; spd++) {
for(var s=0; s<selection.length; s++) {
if(allProductData[apd].indexOf(allProductData[apd][spd]) === selection[s]) {
selectedProductData.push(allProductData[apd][spd])
}
}
}
}
console.log(selectedProductData)
这将返回以下内容
[
"Item1Sku","Item1Name","Item1Price","Item1Available",
"Item2Sku","Item2Name","Item2Price","Item2Available",
"Item3Sku","Item3Name","Item3Price","Item3Available"
]
我想要的是
[
["Item1Sku","Item1Name","Item1Price","Item1Available"],
["Item2Sku","Item2Name","Item2Price","Item2Available"],
["Item3Sku","Item3Name","Item3Price","Item3Available"]
]
任何对此的帮助都会很棒。
答案 0 :(得分:3)
您可以在所需索引处映射数据和值。
const
selection = [0, 1, 3, 4],
allProductData = [['Item1Sku', 'Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'], ['Item2Sku', 'Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'], ['Item3Sku', 'Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']],
selectedProductData = allProductData.map(values => selection.map(i => values[i]));
console.log(selectedProductData);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
使用Array.prototype.reduce
缩小数组,并检查每个当前元素的索引是否在选择数组中,如果确实存在,则将其压入。
const selection = [0, 1, 3, 4];
const allProductData = [
['Item1Sku', 'Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'],
['Item2Sku', 'Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'],
['Item3Sku', 'Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']
];
const selectedProductData = allProductData.reduce((acc, curr) => {
const filtered = curr.filter((product, idx) => selection.includes(idx));
if (filtered.length) {
acc.push(filtered);
}
return acc;
}, []);
console.log(selectedProductData);
答案 2 :(得分:1)
使用for...of
代替i=0;i<x;i++
更具可读性,并且可以帮助您进行修改。
此外,您可以在第一个循环内访问每个元素索引,而不用设置selection
数组。您仍然只会编写一次,并保留一个循环。
var allProductData = [['Item1Sku', 'Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'],['Item2Sku', 'Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'],['Item3Sku', 'Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']];
var selectedProductData = [];
for (let data of allProductData) {
selectedProductData.push([data[0], data[1], data[3], data[4]]);
}
console.log(selectedProductData)
答案 3 :(得分:1)
使用array.map和array.filter。
var selection = [0,1,3,4];
var allProductData = [['Item1Sku','Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'], ['Item2Sku','Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'], ['Item3Sku','Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']];
let result = allProductData.map(data => {
return data.filter((el, ind) => selection.indexOf(ind)!=-1);
})
console.log(result);
答案 4 :(得分:0)
您应该在第一个for循环内创建另一个数组,然后将结果推入该数组,然后将该数组推入最终数组:
var selection = [0,1,3,4];
var allProductData = [['Item1Sku','Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'], ['Item2Sku','Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'], ['Item3Sku','Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']]
var selectedProductData = []
for(var apd=0; apd<allProductData.length; apd++) {
var temp = []; // declare an array here
for(var spd=0; spd<allProductData[apd].length; spd++) {
for(var s=0; s<selection.length; s++) {
if(allProductData[apd].indexOf(allProductData[apd][spd]) === selection[s]) {
temp.push(allProductData[apd][spd]); // push the result
}
}
}
selectedProductData.push(temp); // push the array into the final array
}
console.log(selectedProductData)
答案 5 :(得分:0)
类似下面的代码片段可以完成您想要的工作。 另外请参阅:
let selection = [0,1,3,4];
let allProductData = [['Item1Sku','Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'], ['Item2Sku','Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'], ['Item3Sku','Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']];
let filtered = allProductData.map(item => item.filter((val,index) => selection.includes(index)));
console.log(filtered);
此语法(下面的代码段)在注释中有很多杂音,涉及
filter()
中的回调函数必须计算为true的事实。 MDN 中的实际单词是:
函数是一个谓词,用于测试数组的每个元素。返回 保留元素为true,否则为false
let selection = [0,1,3,4];
let allProductData = [['Item1Sku','Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'], ['Item2Sku','Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'], ['Item3Sku','Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']];
let filtered = allProductData.map(item =>
item.filter((val,index) => {
if(selection.includes(index)) {
return val;
}
})
);
console.log(filtered);
因此,这里有一些示例可以做完全相同的事情而不会引发错误。在所有情况下,.filter()
返回的值都是符合回调函数条件的逻辑真值。
let selection = [0,1,3,4];
let allProductData = [['Item1Sku','Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'], ['Item2Sku','Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'], ['Item3Sku','Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']];
let filtered = allProductData.map(item =>
item.filter((val,index) => {
if(selection.includes(index)) {
return index > 3; // this will return only the itemXAvailability
}
})
);
console.log(filtered);
上面的代码段可以用这种方式重写。
let selection = [0,1,3,4];
let allProductData = [['Item1Sku','Item1Name', 'Item1Desc', 'Item1Price', 'Item1Available', 'Item1Margin'], ['Item2Sku','Item2Name', 'Item2Desc', 'Item2Price', 'Item2Available', 'Item2Margin'], ['Item3Sku','Item3Name', 'Item3Desc', 'Item3Price', 'Item3Available', 'Item3Margin']];
let filtered = allProductData.map(item =>
item.filter((val,index) => selection.includes(index) && index > 3)
);
console.log(filtered);
使用Boolean
true , false 作为要测试的数组的值的另外两个示例。
let selection = [0,1,3,4];
let allProductData = [[true,true, false, true, false, true], [true,true,true,true,true,true], [false,false,false,false,false,false]];
let filtered = allProductData.map(item =>
item.filter((val,index) => selection.includes(index) && index > 3)
);
console.log(filtered);
filtered.forEach((item) => item.forEach((val) => console.log(typeof(val))));
注意输出的差异。
let selection = [0,1,3,4];
let allProductData = [[true,true, false, true, false, true], [true,true,true,true,true,true], [false,false,false,false,false,false]];
let filtered = allProductData.map(item =>
item.filter((val,index) => {
if(selection.includes(index) && index > 3) {
return val;
}
})
);
console.log(filtered);
filtered.forEach((item) => item.forEach((val) => console.log(typeof(val))));
另外两个使用数字的示例。输出不同,其值为0
。
let selection = [0,1,3,4];
let allProductData = [[1,2,3,4,5,6], [6,5,4,3,2,1], [8,8,8,8,0,8]];
let filtered = allProductData.map(item =>
item.filter((val,index) => selection.includes(index) && index > 3)
);
console.log(filtered);
filtered.forEach((item) => item.forEach((val) => console.log(typeof(val))));
let selection = [0,1,3,4];
let allProductData = [[1,2,3,4,5,6], [6,5,4,3,2,1], [8,8,8,8,0,8]];
let filtered = allProductData.map(item =>
item.filter((val,index) => {
if(selection.includes(index) && index > 3) {
return val;
}
})
);
console.log(filtered);
filtered.forEach((item) => item.forEach((val) => console.log(typeof(val))));
使用null
作为我们的值会有什么区别。
let selection = [0,1,3,4];
let allProductData = [[null,null,null,null,null,null], [null,null,null,null,null,null], [null,null,null,null,null,null]];
let filtered = allProductData.map(item =>
item.filter((val,index) => selection.includes(index) && index > 3)
);
console.log(filtered);
filtered.forEach((item) => item.forEach((val) => console.log(typeof(val))));
let selection = [0,1,3,4];
let allProductData = [[null,null,null,null,null,null], [null,null,null,null,null,null], [null,null,null,null,null,null]];
let filtered = allProductData.map(item =>
item.filter((val,index) => {
if(selection.includes(index) && index > 3) {
return val;
}
})
);
console.log(filtered);
filtered.forEach((item) => item.forEach((val) => console.log(typeof(val))));