gpt4 book ai didi

javascript - 获取数组父键或与嵌套项组合

转载 作者:行者123 更新时间:2023-11-30 08:20:39 25 4
gpt4 key购买 nike

我有以下带数组的对象:

{
"brands": [
"accessible-icon",
"accusoft",
"adn",
"adobe",
"adversal",
],
"regular": [
"address-book",
"address-card",
"angry",
"bell",
],
"solid": [
"ad",
"address-book",
"address-card",
"adjust",
"air-freshener",
"align-center",
]
}

我有一个函数,它从一个数组组中返回一个项目。我必须找出该项目是从哪个数组组中提取的。据我了解,不可能在 js 中查找父键?

获取父键是最理想的,否则,是否可以将父键添加到所有嵌套项中?例如,brands 数组中的嵌套项将变为:

 "brands": [
"brands-accessible-icon",
"brands-accusoft",
"brands-adn",
"brands-adobe",
"brands-adversal",
],

然后我可以使用正则表达式来提取brands

更多信息:

最初我将对象转换为具有数组组的对象。以下是名为 brands 的对象之一的示例:

{
"faAccessibleIcon": {
"prefix": "fab",
"iconName": "accessible-icon",
"icon": [
448,
512,
[],
"f368",
]
},
"faAccusoft": {
"prefix": "fab",
"iconName": "accusoft",
"icon": [
640,
512,
[],
"f369",
]
},
"faAcquisitionsIncorporated": {
"prefix": "fab",
"iconName": "acquisitions-incorporated",
"icon": [
344,
512,
[],
"f6af",
]
},
}

然后我简化对象:

const array = {
"brands": Object.keys(brands).map(e=> brands[e].iconName),
"regular": Object.keys(regular).map(e=> regular[e].iconName),
"solid": Object.keys(solid).map(e=> solid[e].iconName),
};

并且 array 成为在我的问题顶部看到的带有数组的对象。数组结构必须保持这样。有没有办法结合 prefixiconName 并获得 brands-accessible-icon, brands-accusoft 等, 在嵌套数组项中,对于每组数组?

最佳答案

您可以获取对象的键并在数组中找到值。

const getKey = (object, value) => Object.keys(object).find(k => object[k].includes(value));

var data = { brands: ["accessible-icon", "accusoft", "adn", "adobe", "adversal"], regular: ["address-book", "address-card", "angry", "bell"], solid: ["ad", "address-book", "address-card", "adjust", "air-freshener", "align-center"] };

console.log(getKey(data, "bell"));

关于javascript - 获取数组父键或与嵌套项组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54143439/

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