gpt4 book ai didi

javascript - 从 JSON 对象中提取深层数据

转载 作者:行者123 更新时间:2023-12-04 10:01:35 24 4
gpt4 key购买 nike

{
"hoodie": {
"body": {
"default": {
"colors": {
"style": {
"solid": {
"colors": {
"body": "body"
}
}
}
}
},
"additional": {
"logo": {
"image": ""
},
"chest-pocket": {
"colors": {
"chest-pocket": "chest-pocket"
}
},
"tag": {
"image": ""
}
}
},
"sleeves": {
"default": {
"colors": {
"style": {
"solid": {
"colors": {
"sleeve": "sleeve"
}
}
}
}
},
"additional": {
"cuffs": {
"colors": {
"cuffs": "cuffs"
}
}
}
}
}
}

我需要的格式是提取附加键。在这种情况下,连帽衫 key 包含作为其 key 的主体和袖子,在附加 key 中包含与附加 key 相对应的不同 key 。我已经给出了需要解析的Object和解析后得到的数据。
{
"body": ["logo", "chest-pocket", "tag"],
"sleeves": ["cuffs"]
}

任何人都可以请分享链接,它可能会帮助我玩对象。
提前致谢。

最佳答案

您可以使用 array.reduceObject.keys 的帮助下获得所需的输出

请检查以下代码段。

const input = {
"hoodie": {
"body": {
"default": {
"colors": {
"style": {
"solid": {
"colors": {
"body": "body"
}
}
}
}
},
"additional": {
"logo": {
"image": ""
},
"chest-pocket": {
"colors": {
"chest-pocket": "chest-pocket"
}
},
"tag": {
"image": ""
}
}
},
"sleeves": {
"default": {
"colors": {
"style": {
"solid": {
"colors": {
"sleeve": "sleeve"
}
}
}
}
},
"additional": {
"cuffs": {
"colors": {
"cuffs": "cuffs"
}
}
}
}
}
}

let output = Object.keys(input.hoodie).reduce((acc, curr) => {
acc[curr] = Object.keys(input.hoodie[curr].additional)
return acc
}, {})

console.log(output)

关于javascript - 从 JSON 对象中提取深层数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61799445/

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