ES6 map an array of objects, to return an array of objects with new keys [duplicate]

I have an array of objects:

[ { id: 1, name: 'bill' }, { id: 2, name: 'ted' } ] 

Looking for a simple one-liner to return:

[ { value: 1, text: 'bill' }, { value: 2, text: 'ted' } ] 

So I can easily pump them into a react dropdown with the proper keys.

I feel like this simple solution should work, but I'm getting invalid syntax errors:

this.props.people.map(person => { value: person.id, text: person.name }) 
1

1 Answer

You just need to wrap object in ()

var arr = [{ id: 1, name: 'bill' }, { id: 2, name: 'ted' }] var result = arr.map(person => ({ value: person.id, text: person.name })); console.log(result)
7

You Might Also Like