[Solved] How to slice every array that are within an array JavaScript [closed]


Use a forEach() loop or map() and simply push the sliced original array items into the second array.

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

Using a map() to get the same result

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

1

solved How to slice every array that are within an array JavaScript [closed]