Convert object to an array of objects?

前端 未结 3 1465
无人共我
无人共我 2021-01-13 15:30

I have an object that looks like this:

{
  \"1\": \"Technology\",
  \"2\": \"Startup\",
  \"3\": \"IT\",
}

and I need to convert it to an a

3条回答
  •  天涯浪人
    2021-01-13 15:54

    You can use .map() with Object.keys():

    let data = {
        "1": "Technology",
        "2": "Startup",
        "3": "IT",
    };
    
    let result = Object.keys(data)
                       .map(key => ({id: Number(key), name: data[key]}));
    
    console.log(result);

    Useful Resources:

    • Array.prototype.map()
    • Object.keys()

提交回复
热议问题