I have a JSON object, color, that consists of two JSON objects. I need to transfer these two JSON objects to an array. How can I do that?
i.e. {blue, purple} to [blue, purple]
I have a JSON object, color, that consists of two JSON objects. I need to transfer these two JSON objects to an array. How can I do that?
i.e. {blue, purple} to [blue, purple]
The need for this is probably a code smell, but here you go:
const colorObject = {
blue: { "foo": "bar" },
purple: { "baz": "qum" }
};
const colorArray = Object.keys(colorObject).map(k => colorObject[k]);
console.log(colorArray);
As James Thorpe mentions in the comment above, if you can do this once without keeping the array around, that's preferable.
let obj = {blue: {foo: 'bar'}, purple: {baz: 'qux'}};
let arr = Object.keys(obj).map(key => obj[key]);
console.log(arr);