1

I am sure this an elementary question, apologies for asking. I've had a good time searching on the this with no luck... I am looking for the following object transformation.

var test= { one: 1, two: 2, three: 3}

Into this:

var test= [{ one: 1},{ two: 2},{ three: 3}]

Any help will be greatly appreciated, thanks!

3 Answers 3

3

Use Object.keys and Array map

var test = { one: 1, two: 2, three: 3}

var objArray = Object.keys(test).map(item => ({ [item]: test[item] }))

console.log(objArray)

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

Comments

2

with help of entries and map:

var test= { one: 1, two: 2, three: 3};
var result = Object.entries(test).map(([k,v])=>({[k]:v}));

console.log(result);

Comments

2

You can use Object.keys() combined with .map(), here is a working snippet:

var test= { one: 1, two: 2, three: 3}

let newArr = Object.keys(test).map((el) => ({[el]: test[el]}));
console.log(newArr);

1 Comment

It's now a 1:1 copy of the accepted answer, but at least it works

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.