I need to flat arrays but i can't to use flat().
First check example of my arrays
let arr = [
['test1' , 'test1'],
['test2' , 'test2'],
['test3', true],
['test4' , false]
];
What is problem here?
I need to get only first item 'test1' , 'test2', 'test3', 'test4' and push to one array
After that I want to my array be;
['test1' , 'test2' , 'test3' , 'test4' ];
What I'm try:
let arr = [
['test1' , 'test1'],
['test2' , 'test2'],
['test3', true],
['test4' , false]
];
let newArr = arr.flat();
but I got all items not first index in each array
CodePudding user response:
Using Array#map
:
const arr = [ ['test1' , 'test1'], ['test2' , 'test2'], ['test3', true], ['test4' , false] ];
const res = arr.map(([ e ]) => e);
console.log(res);
CodePudding user response:
You need to use Array.map()
first,then invoke Array.flat()
let arr = [
['test1' , 'test1'],
['test2' , 'test2'],
['test3', true],
['test4' , false]
];
let result = arr.map(i => i[0]).flat()
console.log(result)
CodePudding user response:
Try this :
let arr = [
['test1' , 'test1'],
['test2' , 'test2'],
['test3', true],
['test4' , false]
];
console.log(arr.map(innerArr => innerArr[0]));