例如我有一个数组
humans:[
dave: {
...daveData
},
mike: {
...mikeData
}
]
现在调用 human[0]
返回 {..daveData}
但如何获取表示 的 KEY 的 NAME 'dave'
、'mike'
作为字符串...抱歉,如果我重复自己,但我没有找到类似的问题。
还要说的是,我使用 React 来使用 array.map() 渲染
标签
最佳答案
你的数组无效,假设从注释中你的数组是这样的:
const humans = [
{
dave: 'bar' // or whatever, doesn't need to be a string
},
{
mike: 'baz'
}
];
那么你可以:
const humans = [
{
dave: 'bar'
},
{
mike: 'baz'
}
];
const myKeys = humans.map(x => Object.keys(x)[0]);
console.log(myKeys)
关于javascript - 获取对象数组中键的名称 - JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48641989/