I want to access all values of an array of objects
let data = [
{ firstName1: 'a', id: 1, email1: '[email protected]'},
{ firstName2: 'b', id: 2, email2: '[email protected]'}
];
data.map(ele=> <p>{ ele.firstName}</p>)
result should be:
a
b
CodePudding user response:
Accessing keys dynamically
let data = [
{ firstName1: 'a', id: 1, email1: '[email protected]'},
{ firstName2: 'b', id: 2, email2: '[email protected]'}
];
Object.keys(data).map(key => <p>{data[key]}</p>)
CodePudding user response:
There are multiple ways to extract key/value from objects in javascript
data.forEach( dataEle =>{
for (const [key, value] of Object.entries(dataEle)) {
console.log(key ": " value)
}
})
Or another way like this
data.forEach( dataEle =>{
Object.keys(dataEle).forEach(key =>{
console.log(key ": " dataEle[key])
})
})