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]])
}
Try this:
let arr = [["1", 2], ["3", 2], ["6", 2]];
let res = arr.map(val => {
return val[0]
})
console.log(res);
arr.map(val => val[0])
@mplungjan edited :). Btw just want to know what was the issue with that?
@SaurabhAgrawal - I think mplungjan was pointing out that you can use a concise arrow function there instead of a verbose one:
.map(val => val[0])
. (Yes, the()
you edited out are optional as well, but I don't think that was the main point.)@SaurabhAgrawal - No, they do not. They're purely part of the syntax of an arrow function parameter list. They're optional when there's exactly one parameter. It makes no different whatsoever to the execution of the function.
@SaurabhAgrawal - I suppose, he meant Extra
()
was not needed as you were using only single argument :)