jfox
jfox

Reputation: 898

Accessing object key inside _.map

My data is structured like this currently

data = {
  a2321sdrf: {
    title: 'x',
    content: 'asad'
  },
  asdasd23: {
    title: 'x',
    content: 'asad'
  },
  dfxdsfds: {
    title: 'x',
    content: 'asad'
  }
}

Then I'm using lodash map to iterate through to output jsx. But, I can't figure out how to access the object key, to use as a key prop.

renderMenu() {
  return _.map(this.props.data, item => {
    return (
      <h1 key={:?}>{item.title}</h1>
    );
  });
}

Upvotes: 0

Views: 64

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

The key will be the second value to the iteritee function:

return _.map(this.props.data,
     (item, key) => { return ( <h1 key={key}>{item.title}</h1> );

Upvotes: 4

Related Questions