admin管理员组文章数量:1333442
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]])
}
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]])
}
Share
Improve this question
edited Dec 5, 2019 at 10:01
deHaar
18.6k11 gold badges45 silver badges55 bronze badges
asked Dec 5, 2019 at 9:58
RafałRafał
832 bronze badges
5 Answers
Reset to default 8You could just use a simple map
and destructure the first element:
const arr = [["1", 2],["3", 2],["6", 2]]
console.log(arr.map(([e]) => e))
The ([e])
part of that before the =>
is destructuring the parameter using array destructuring. It means that for each subarray passed to the map
callback, e
receives the value of the first element of the subarray. In ES5 and earlier, the ([e]) => e
arrow function would be function(entry) { return entry[0]; }
Still, if you still don't understand the concept, prefer efficiency, or just want to go back to basics, you could use the trusty for loop, making sure to push only the first element of each subarray:
const arr = [["1", 2],["3", 2],["6", 2]]
const output = []
for (let i = 0; i < arr.length; i++) {
output.push(arr[i][0])
}
console.log(output)
Try this:
let arr = [["1", 2], ["3", 2], ["6", 2]];
let res = arr.map(val => {
return val[0]
})
console.log(res);
You can use Array.prototype.map() to crate a new array with the item from the first index:
var arr = [["1",2],["3",2],["6",2]]
var newArray = arr.map(i => i[0]);
console.log(newArray);
This one also works
console.log(Object.keys(Object.fromEntries([["1", 2],["3", 2],["6", 2]])))
In this example, Object.fromEntries
will create an object from an array of key/value pairs - it will take the first element as a key, and the second element as the value - creating something like this:
{
"1": 2,
"3": 2,
"6": 2
}
Then, Object.values
will grab the keys of the object, thus, removing the values and retaining the keys, giving the desired output.
P/S: just added another way to do this
console.log(Array.from([["1", 2],["3", 2],["6", 2]], x=>x[0]))
Use map
and get the first element using shift
method.
PS: not very efficient because of ...(spread) operator for each element.
const arr = [["1",2],["3",2],["6",2]];
const arrFirsts = arr.map(items => [...items].shift());
console.log(arrFirsts)
console.log(arr)
本文标签: javascriptHow can I made new array from firsts elements of arrays from an arrayStack Overflow
版权声明:本文标题:javascript - How can I made new array from firsts elements of arrays from an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742354731a2459183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论