-1

I have an object that I receive from an api, and each object's length are similarly long.
now, it does have a key fname and lname and I want it to become just name. I don't want to manually create an empty array and push a new one there just to change fname and lname into name.

Is there anyway I can achieve this without looping through the object and manually pushing it?

3

1 Answer 1

1

You can destructure the object while mapping it, here I have created a dummy data, let me know if this is something what you need:

var array=[{id:1, fname:'Bob', lname: 'Alice', someotherkey:'key'}]; var result = array.map(({fname, lname, ...rest})=>({name:fname+' '+lname, ...rest})); console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

Instead of adding just another answer on "how to rename a property", look for a dupe and VtC. Your approach with .map() and the spread syntax will most likely be part of those duplicates (see the examples in the comments below the question)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.