Interception of nested arrays

var arr=[
    [11,12,13,"","2"],
    [21,22,23,"","2"],
    [31,32,33,"","2"]
];
var arr2=[
    [11,12,13],
    [21,22,23],
    [31,32,33]
];

I want to ask everyone for a simple method. The result of intercepting the first three bits of each item in arr, such as arr2
, can only create an empty array by itself. After processing the data, put it into arr2

.
May.29,2021

var arr2 = arr.map(cur => cur.slice(0, 3))
// Array.from
arr2 = Array.from(arr, item => item.slice(0, 3))

arr.forEach(value => {
  value.splice(3);
});

this directly modifies the original arr

let arr2 = arr.map(value => {
  return value.slice(0,3);
});

this does not change the original array, but the new array meets the requirements


this kind of one-to-one mapping of data processing, you can use the logic of map, to process data and put it into the callback function of map, for example:

arr2 = arr.map(function(e){
    return e.slice(0, 3)
})

if written in this way, it not only has clear logic and high readability, but also always returns the new array and does not modify the old array, so it has the characteristics of pure function.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3782b-2c0b2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3782b-2c0b2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?