-3

I have a object array in javascript. Example:

objArr = [{"FirstName":"John","LastName":"Doe","Age":35},{"FirstName":"Jane","LastName":"Doe","Age":32}]

I want to create an object array like this

newObjArr=[{"Name":"John Doe","Age":35},{"Name":"Jane Doe","Age":32}]

How should I do this?

3

1 Answer 1

2

You can use Array.map

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

const objArr = [{"FirstName":"John","LastName":"Doe","Age":35},{"FirstName":"Jane","LastName":"Doe","Age":32}]


const newObjArr = objArr.map(({FirstName, LastName, Age}) => {
    return { 
      Name: `${FirstName} ${LastName}`,
      Age
    };
});

console.log(newObjArr);

Or an ugly one liner:

objArr.map(({FirstName, LastName, Age}) => ({ Name: `${FirstName} ${LastName}`, Age }));
Sign up to request clarification or add additional context in comments.

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.