例如此数组
'APP_DIRS': True
我想切片每个数组以使用slice(0,3)生成一个像这样的新数组
DIRS
答案 0 :(得分:1)
使用forEach()循环或map()并将切片的原始数组项简单地推入第二个数组中。
var array= [
[1,"dataA","dataB","dataC","dataD"],
[2,"dataA","dataB","dataC","dataD"],
[3,"dataA","dataB","dataC","dataD"],
[4,"dataA","dataB","dataC","dataD"]
];
var resultSecondArray= [];
array.forEach (function(arr){
resultSecondArray.push(arr.slice(0,3));
})
console.log(resultSecondArray); // gives [[1, "dataA","dataB"],[2, "dataA","dataB"],[ 3, "dataA", "dataB"],[ 4,"dataA", "dataB"]]
使用map()获得相同的结果
let array= [
[1,"dataA","dataB","dataC","dataD"],
[2,"dataA","dataB","dataC","dataD"],
[3,"dataA","dataB","dataC","dataD"],
[4,"dataA","dataB","dataC","dataD"]
];
let resultSecondArray= array.map(arr => arr.slice(0,3));
console.log(resultSecondArray); // gives [[1, "dataA","dataB"],[2, "dataA","dataB"],[ 3, "dataA", "dataB"],[ 4,"dataA", "dataB"]]