-4

I have an array that looks something like this:

[ { Id:1, Name:'' }, { Id:2, Name:'' }, { Id:2, Name:'' } ]

I want a result object that looks like this:

{ Id:[1, 2, 3] }

How do I achieve this in Javascript?

2

2 Answers 2

4

You can create an Object Literal and use Array.prototype.map() to get ids array to fulfill the Id property.

Code:

const data = [ { Id: 1, Name:'' }, { Id: 2, Name:'' }, { Id: 3, Name:'' } ]
const result = { Id: data.map(obj => obj.Id) };

console.log(result);

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

Comments

4
const array = [ { Id:1, Name:'' }, { Id:2, Name:'' }, { Id:2, Name:'' } ];
console.log({Id: array.map(element => element.Id)})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.