Reputation: 173
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!
Upvotes: 1
Views: 184
Reputation: 11622
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);
Upvotes: 2
Reputation: 5308
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);
Upvotes: 2
Reputation: 3107
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)
Upvotes: 3