1

I have an object which has objects inside in it at the following form:

Object { 
       "item1": { "subitem1": 5, "subitem2": 10 },
       "item2": { "subitem1": 3, "subitem2": 12, "subitem3": 1 },
       "item3": { "subitem1": 8, "subitem2": 1, "subitem3": 3 }
       }

I want to convert it to an array with the following form:

[0] Object { key: "item1", "subitem1": 5, "subitem2": 10 }
[1] Object { key: "item2", "subitem1": 3, "subitem2": 12, "subitem3": 1 }
[2] Object { key: "item3", "subitem1": 8, "subitem2": 1, "subitem3": 3 }

Any ideas? Thanks in advance

2
  • 2
    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a minimal reproducible example. For more information, please see How to Ask and take the tour. Commented Dec 5, 2018 at 9:45
  • The order in which you get keys/values of an object is not guaranteed so you can't convert the object to an array where the first element happens to be item1, at least not according to the specifications. The answers may work by accident but can break at any moment if implementation of JS interpreter changes. Commented Dec 5, 2018 at 9:52

3 Answers 3

2

You could get the entries of the object and assign the key.

var object = { item1: { subitem1: 5, subitem2: 10 }, item2: { subitem1: 3, subitem2: 12, subitem3: 1 }, item3: { subitem1: 8, subitem2: 1, subitem3: 3 } },
    result = Object.entries(object).map(([key, o]) => Object.assign({ key }, o));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

Order of keys/values/entries for ... in or for ... of in object is not guaranteed, you can't depend on the resulting array to be in any particular order.
@HMR, actually, yes it follows some rules, like integer, which could be indices for an array are sorted first and then all keys are sorted by insertation order.
The point was that you should warn about this since the specification says arbitrary order. It happens to get the key/values/entries in the order that they are added/specified but there is no guarantee. If order is important then another data type should be chosen or the resulting array should be sorted.
1

You can use Object.keys() and Array.prototype.map():

const obj = { "item1": { "subitem1": 5, "subitem2": 10 }, "item2": { "subitem1": 3, "subitem2": 12, "subitem3": 1 }, "item3": { "subitem1": 8, "subitem2": 1, "subitem3": 3 }},
res = Object.keys(obj).map(key => ({key, ...obj[key]}));

console.log(res);

Do note, however, the order of the keys retrieved from Object.keys is not guaranteed to be the same order as listed in your object

3 Comments

Order of keys in object is not guaranteed, you can't depend on the resulting array to be in any particular order.
Would also advice using forEach if you are mutating something. The map function is usually used with pure callback function.
@HMR okay, I change it to be a pure callback now
0

You can try this mate

let obj = { "item1": { "subitem1": 5, "subitem2": 10 }, "item2": { "subitem1": 3, "subitem2": 12, "subitem3": 1 }, "item3": { "subitem1": 8, "subitem2": 1, "subitem3": 3 } };

let op =[];
for(let key in obj){
  op.push({
    key : key,
    ...obj[key]
  })
}
console.log(op);

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.