我该如何获取数组的所有索引?
[
{
"name":"aloha",
"age":"18"
},
{
"name":"hello word"
},
{
"name":"John Doe",
"age":"28"
}
]
输出应该像[0,1,2]。
我该如何获取数组的所有索引?
[
{
"name":"aloha",
"age":"18"
},
{
"name":"hello word"
},
{
"name":"John Doe",
"age":"28"
}
]
最简单的方式是(参见此文章):
let a = [{1: 'x'}, {1: 'y'}, {1: 'z'}]
let b = Array.from(a.keys())
console.log(b)
如果想要实现此功能,最简单的方法是在数组上调用 map((_, i) => i)
:
let a = [{1: 'x'}, {1: 'y'}, {1: 'z'}]
let b = a.map((_, i) => i)
console.log(b)
let a = [
{
'name' : "aloha",
"age": "18"
},
{
"name": "hello word"
},
{
"name": "John Doe",
"age" : "28"
}]
console.log(Object.keys(a));
//The array you want to get all the indexes from
const array = [{'a':1}, {'b':2}, {'c':3}];
//All indexes array
const indexArray = [];
array.forEach((el, i) => {
indexArray.push(i);
});