Skip to content
Advertisement

Flatten dictionary of strings and dictionaries

By a dictionary I mean an object. For example, there is such a dictionary:

dict = {
  "foo": {qty: 1, price: 23},
  "bar": {qty: 7, price: 182}
 }

How do i get

arr = [["foo", 1, 23],
        ["bar", 7, 182]]
  

Answer

You can use Object.entries and Object.values for that:

const dict = {"foo": {qty: 1,price: 23},"bar": {qty: 7,price: 182}};

const result = Object.entries(dict).map(([k, v]) => [k, ...Object.values(v)]);

console.log(result);
Advertisement