I have an array for instance
humans:[
dave: {
...daveData
},
mike: {
...mikeData
}
]
now calling human[0]
returns {..daveData}
but how can I get the NAME of the KEY meaning 'dave'
, 'mike'
as a string ... sorry if I'm repeating myself, but I didn't find similar questions.
Also to say im using React to render
tags using array.map()
Your array is invalid, assuming from the comments your array is like this:
const humans = [
{
dave: 'bar' // or whatever, doesn't need to be a string
},
{
mike: 'baz'
}
];
then you could:
const humans = [ { dave: 'bar' }, { mike: 'baz' } ]; const myKeys = humans.map(x => Object.keys(x)[0]); console.log(myKeys)
You can use reduce
function and spread operator
.
var data = [{dave: {}}, {mike: {}}, {'Георги': {}}, {'Димитранов': {}}]; var results = data.reduce((a, c) => [...a, ...Object.keys(c)], []); console.log(results);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.