0

I have an array in the format

[{"Name":"abc","mark":[10,20,30]},{"Name":"def","mark":[10,20,30]}]

is there a way to convert this array in the following format in node js i tried iterating over and pushing but still not able to get it in following format,

{"abc":[10,20,30],"def":[10,20,30]}

Can any one help me in this?

2 Answers 2

1

You could use a simple forEach to loop inside the array and create result object like:

const input = [{"Name":"abc","mark":[10,20,30]},{"Name":"def","mark":[10,20,30]}];
let result = {};
input.forEach(x => {
  result[x.Name] = x.mark;
})
console.log(result)

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

Comments

0

You can use reduce to get the desired output

const input = [{"Name":"abc","mark":[10,20,30]},{"Name":"def","mark":[10,20,30]}];
const output = input.reduce((acc, curr) => { return {...acc, [curr.Name]: curr.mark}}, {})

Documentation

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.