user2824411 user2824411 - 11 days ago 6
React JSX Question

immutableJS+React Get key of iterable while mapping

There is OrderedMap immutablejs object:

{
"country": {
"value": "US",
"locked": false,
"history": null
},
"city": {
"value": "NY",
"locked": false,
"history": [
{
"value": "LA",
"date": 1447366980
},
{
"value": "WA",
"date": 1447331640
}
]
},

"event_title": {
"value": "some text",
"locked": false,
"history": null
},
"place": {
"value":"some text",
"locked": false,
"history":null
}
}


Need to map it to React components:

const fields = res.map(item => <Field name={%KEY_NEED_HERE%} rest={item} handleChange={this.handleChange} />)


How to get key of iterable and send it to name of Field? Somethng like

<Field name={city} rest={item} />


Plz, help

Answer

The 2nd argument passed to the mapper is the key, so you can do this:

const fields = res.map((item, key) => <Field name={key} rest={item} handleChange={this.handleChange} />)