-1

Now I have object like this

 let arr = [{name:'first'},{name:'first1'}] 

Exprected

 [{value:'first'},{value:'first1'}] 

My attempt

 let result = arr.map(el=>{ const {name, ...other} = el; return {value: el.name, ...other} }) 
1
  • Your code works. What's the issue? Commented Apr 27, 2021 at 7:14

2 Answers 2

2

you can do it that way!

let arr = [{name:'first'},{name:'first1'}] //{value: 'first'} let result = arr.reduce((acc,el)=>{ return [...acc, {'value' : el.name}] },[]) 
Sign up to request clarification or add additional context in comments.

2 Comments

What is wrong with OP's code. Why use reduce instead of map when the input and the output have the same number of elements?
It's a matter of preference if the length of output and input array is the same. otherwise reduce is an aggregate function (return a single value in the end), map returns an array of transformed values.
0

You can transform the data using Array.prototype.map.

let arr = [{name:'first'},{name:'first1'}] let result = arr.map(x => {return {value: x.name}}); console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.