2

I have the following object :

let categories = {
  "Asset Type": ["appliances", "electronics"],
  "Asset Availability": ["in stock"]
}

I need to convert it to the following structure :

[
   {
     "name": "Asset Type",
     "values": ["Appliances", "Electronics"]
    }, 
    {
      "name": "Asset Availability",
      "values": ["In Stock"]
    }
]
2
  • Note that you can’t make any assumptions about the order of the resulting array elements. Commented Apr 3, 2018 at 11:30
  • 1
    @SebastianSimon If you want to be guaranteed by the spec, you can, but only if you use the right methods (like Reflect.ownKeys). If you just want the result to be dependable, you can use any of the object iteration methods - although the spec doesn't guarantee it, environments use the same algorithm anyway, luckily. (array indicies, followed by insertion order strings, followed by insertion order symbols) Commented Sep 25, 2019 at 8:43

5 Answers 5

9

Use Object.entries, array.prototype.map and some destructuring:

var categories = {
  "Asset Type": ["appliances", "electronics"],
  "Asset Availability": ["in stock"]
 };
 
var res = Object.entries(categories).map(([name, values]) => ({name, values}));

console.log(res);

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

4 Comments

I found a duplicate target to this question, but this answer is more elegant than any other.
Can you explain me why we have to surround the return object with () ?
if its not surrounded by (). {} means the function body after fat arrow. so it will not treat it as object return.
Without the (), javascript will interpret {} as a block, instead of object
2

The approach i like the most is to:

  1. Call the map function on each key of the object.
  2. Create a new object with the property "name" set to the key and the value to the corresponding value

Please note that the key has to be unique in this approach (which it should be anyways):

var categories = {
  "Asset Type": ["appliances", "electronics"],
  "Asset Availability": ["in stock"]
 };

var res = Object.keys(categories).map(key => ({
      name: key,
      values: categories[key]
    });
);

console.log(res);

Comments

2

const categories =  {
  "Asset Type": ["appliances", "electronics"],
  "Asset Availability": ["in stock"]
 }

const toArray = obj => 
  Object.keys(obj).reduce((acc, key) => [...acc, {name: key, values: obj[key]}], [])

console.log(toArray(categories));

Comments

0

Try like this ==>

var obj = {
    "categories":
         {
          "Asset Type": ["appliances", "electronics"],
          "Asset Availability": ["in stock"]
         },
};


var categories = [];
for(let p in obj.categories) {

    categories.push({
        name: p,
        values : obj.categories[p]
    });
}

console.log(categories);

Comments

0

you can try something like this or use map function there is a lot of ways

let obj = {
    "categories":
        {
            "Asset Type": ["appliances", "electronics"],
            "Asset Availability": ["in stock"]
        }

}
let categories=[];
Object.keys(obj.categories).forEach(key=>{
    categories.push({"name":key,"value":obj.categories[key]});
});
console.log(categories);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.