gpt4 book ai didi

javascript - 具有多个级别的过滤器数组

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

我需要遍历一个对象数组来比较两个值。这是我的数组:

    const data = [{
"uid": "6448ae4a79",
"title": "New Group",
"slug": "new-group",
"items": [
{
"uid": "8602b1cf1f",
"title": "New sub group",
"slug": "new-sub-group",
"items": [
{
"uid": "8b6f962ff1",
"_id": "5d9b453285a8982000b4a9e6",
"title": "fsds",
"choices": [
{
"uid": "a5ccb273a2",
"_id": "5d9b453285a8982000b4a9eb",
"type": null,
"text": "sd",
"label": null,
"value": null
},
{
"uid": "8ab0d45386",
"_id": "5d9b453285a8982000b4a9e8",
"type": null,
"text": "sdwewe",
"label": null,
"value": null
}
]
},
{
"uid": "290db30b53",
"_id": "5d9b699185a8982000b4aa1d",
"title": "czxczxc",
"logic": {
"viewControl": [
{
"action": "lock",
"group": "6448ae4a79",
"subGroup": "8602b1cf1f",
"question": "8b6f962ff1",
"equalTo": "8ab0d45386"
}
]
},
"choices": [
{
"uid": "3cce36d426",
"_id": "5d9b699185a8982000b4aa1f",
"type": 0,
"text": "wqewq",
"label": "dsfsd",
"value": null
}
]
}
]
}
]
}]

我必须将 items.items.logic.viewControl[0].equalTo 与 Choices.uid 进行比较。例如:“equalTo”:“8ab0d45386”===“uid”:“8ab0d45386”。

我该怎么做?我尝试了filter、map和foreach,但根本做不到

最佳答案

如果您的目标是检查 viewControl.equalTo - 是选择之一(来自不同的项目) - 那么解决方案可能如下所示:

  1. 检查所有项目并收集 choices.uid
  2. 检查所有 viewControls 并检查 choices.uid 是否存在。

const data = [{
"uid": "6448ae4a79",
"title": "New Group",
"slug": "new-group",
"items": [
{
"uid": "8602b1cf1f",
"title": "New sub group",
"slug": "new-sub-group",
"items": [
{
"uid": "8b6f962ff1",
"_id": "5d9b453285a8982000b4a9e6",
"title": "fsds",
"choices": [
{
"uid": "a5ccb273a2",
"_id": "5d9b453285a8982000b4a9eb",
"type": null,
"text": "sd",
"label": null,
"value": null
},
{
"uid": "8ab0d45386",
"_id": "5d9b453285a8982000b4a9e8",
"type": null,
"text": "sdwewe",
"label": null,
"value": null
}
]
},
{
"uid": "290db30b53",
"_id": "5d9b699185a8982000b4aa1d",
"title": "czxczxc",
"logic": {
"viewControl": [
{
"action": "lock",
"group": "6448ae4a79",
"subGroup": "8602b1cf1f",
"question": "8b6f962ff1",
"equalTo": "8ab0d45386"
}
]
},
"choices": [
{
"uid": "3cce36d426",
"_id": "5d9b699185a8982000b4aa1f",
"type": 0,
"text": "wqewq",
"label": "dsfsd",
"value": null
}
]
}
]
}
]
}];

// 1. Collect choices.uid
const choicesUids = {};
data.forEach((d) => {
d.items.forEach((i1) => {
i1.items.forEach((i2) => {
i2.choices.forEach(c => {choicesUids[c.uid]=true})
});
});
});

// 2. Check the logic
data.forEach((d, ix0) => {
d.items.forEach((i1, ix1)=>{
i1.items.forEach((i2, ix2)=>{
if(i2.logic && i2.logic.viewControl) {
i2.logic.viewControl.forEach((vc, ix3)=>{
console.log(`data[${ix0}].items[${ix1}].items[${ix2}].logic.viewControl[${ix3}].equalTo:${vc.equalTo} - is ${choicesUids[vc.equalTo]?'GOOD':'BAD'}`);
});
}
});
});
});

输出:data[0].items[0].items[1].logic.viewControl[0].equalTo:8ab0d45386 - 很好

关于javascript - 具有多个级别的过滤器数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58290704/

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