Get item in sub array and put back to main array
Instead of join()
use Array.prototype.flat()
on the returned result of map()
:
var array_demo = [
['1st', '1595', '8886'],
['2nd', '1112']
]
array_demo = array_demo.map(function(i) {
return i.slice(1);
}).flat();
console.log(array_demo);
In one liner using Arrow function(=>):
var array_demo = [
['1st', '1595', '8886'],
['2nd', '1112']
]
array_demo = array_demo.map(i => i.slice(1)).flat();
console.log(array_demo);
You can use Array.prototype.flat
followed by a Array.prototype.filter
to get rid of the non-numeric data in your subarrays:
var array_demo = [
['1st', '1595', '8886'],
['2nd', '1112']
];
const res = array_demo.flat().filter(n=> !isNaN(n));
console.log(res);
We could do it using Array.prototype.reduce
also, if your objective is to remove only the object at the 0th
index:
var array_demo = [
['1st', '1595', '8886'],
['2nd', '1112']
];
const res = array_demo.reduce((r, [_, ...e]) => { r.push(...e); return r}, []);
console.log(res);
You could destructure the array and take the rest without the first element and flatten this array using Array.flatMap
.
var array = [['1st', '1595', '8886'], ['2nd', '1112']],
result = array.flatMap(([_, ...a]) => a);
console.log(result);
Alternatively Array.slice()
works as well.
var array = [['1st', '1595', '8886'], ['2nd', '1112']],
result = array.flatMap(a => a.slice(1));
console.log(result);