Back
Below is the array of objects I’ve:
[ { id: 1, name: 'bill' }, { id: 2, name: 'ted' }]
[
{
id: 1,
name: 'bill'
},
id: 2,
name: 'ted'
}
]
I want the below output in a simple one-liner:
[ { value: 1, text: 'bill' }, { value: 2, text: 'ted' }]
value: 1,
text: 'bill'
value: 2,
text: 'ted'
Can anyone tell me how to do this?
Use () to wrap the object:
var arr = [{ id: 1, name: 'bill'}, { id: 2, name: 'ted'}]var result = arr.map(person => ({ value: person.id, text: person.name }));console.log(result)
var arr = [{
}, {
}]
var result = arr.map(person => ({ value: person.id, text: person.name }));
console.log(result)
30.9k questions
32.9k answers
500 comments
665 users