gpt4 book ai didi

javascript - 如何将对象转换为对象数组

转载 作者:行者123 更新时间:2023-12-02 17:28:54 25 4
gpt4 key购买 nike

我有以下对象:

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

我需要将其转换为以下结构:

[
{
"name": "Asset Type",
"values": ["Appliances", "Electronics"]
},
{
"name": "Asset Availability",
"values": ["In Stock"]
}
]

最佳答案

使用Object.entriesarray.prototype.map和一些解构:

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

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

console.log(res);

关于javascript - 如何将对象转换为对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49628432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com