ES6 map throws error on object array
1 answer
You forgot to use ()
, write it like this:
const normalizeEventTypes = nextProps.events.space_events.map((obj, i) =>
obj.id
)
Reason :
You are using obj and indexing both parameters in the callback function map
, so you need to use ()
to wrap parameters like:
a = b.map((i,j) => i)
These ()
are optional if we only want to use one parameter, for example:
a = b.map(i => i)
Various ways to use map
:
1. a.map(i => i + 1); //when index is not required
2.
a.map(i => { //when want to do some calculation
//some calculation
return //something;
})
3. a.map((i,j) => i + j) //when want to use the index of item
Check out the working snippet:
let data = {
"space_events": [{
"id": 1,
"name": "Anniversaries"
},
{
"id": 2,
"name": "Brand Rollout"
}
]
}
let result = data.space_events.map((obj,i) => obj.name);
console.log('result', result);
+5
source to share