Warm tip: This article is reproduced from stackoverflow.com, please click
arrays javascript

How can I made new array from firsts elements of arrays from an array

发布于 2020-03-27 15:45:21

How can I made new array from firsts elements of arrays from this array ?

[["1",2],["3",2],["6",2]] 

and I want it to be

['1', '3', '6'] 

My attempt:

var newArray = []

for (i = 0; i < arrayToCompare.length - 1; i++) {
    newArray.push(arrayToCompare[[0]])
}
Questioner
Rafał
Viewed
16
Saurabh Agrawal 2019-12-05 18:03

Try this:

let arr = [["1", 2], ["3", 2], ["6", 2]];
let res = arr.map(val => {
    return val[0]
})
console.log(res);