javascript - 打破嵌套的一些或映射 JavaScript
问题描述
PC = {a:{ID: "abc",options:{x1:"100", x2:"200"}},b:{ID: "d",options:{x2:"100", x3:"200"}}}
pro = {
"pro": [
{
"pID": "abc",
"attributes": {
"xyz": [
"1",
"2",
"3"
],
"foo": "フルプレミアム"
}
}
]
}
functionX() {
let isND = true;
if (pro === null || pro === [] || pro.length === 0) {
return isND;
} else if (pro.length > 0) {
some(PC, (p) => {
some(p.options, (o, k) => {
some(pro, (item) => {
if (p.ID === item.pID && k === 'xyz') {
if (item.attributes[k] !== []) {
isND = false;
}
} else if (p.ID === item.pID && k !== 'xyz') {
if (item.attributes[k] !== '') {
isND = false;
}
}
});
});
});
}
return isND;
}
我必须遍历 3 个不同的集合来检查我的状况并返回一个值。如果我的 if-else 条件之一满足,我正在尝试退出嵌套的 some or map。我尝试在 isND = false 之后传递 return true 但不起作用。有人可以帮助解决这个问题。
解决方案
Array.prototype.some()
如果任何回调返回,将提前退出,true
这样你就可以return
得到结果。
这不是很清楚,但似乎您想在返回逆向时使用此“提前退出”功能。像这样的东西怎么样...
// ignoring "if (pro === null || pro === [] || pro.length === 0)" for this example
// return the inverse
return !Object.values(PC).some(({ ID, options }) => {
return Object.entries(options).some(([k, o]) => {
// here "k" is one of your "x1", "x2", etc keys
// and "o" is the corresponding value
return pro.pro.some(item => {
// return "true" if any of your "conditions" are met
})
})
})
推荐阅读
- pandas - 如何进行布尔索引但同时将两个子集分配给一个变量?
- python - setuptools 和源代码分发的“构建步骤”是什么意思?
- c - 链表 printf("%d\n",tasks_head->head->tid) 分割错误
- sas - 将宏 DO 循环与数据步 DO 循环混合
- eclipse - 升级到 Java 11 时尝试将 javax.xml.soap 添加到 RCP
- sql - SQL IN 在哪里。优化器有多聪明?
- pandas - 当该数据帧的一列包含该数据帧中另一列的子字符串时,有没有办法只保留数据帧中的行?
- amazon-web-services - Localstack + aws cli 2:在收到来自端点 url http://localhost:4572 的有效响应之前,面临和错误连接已关闭
- python - 分类编码后如何解决训练集和测试集的不匹配问题?
- excel - 如何使我的代码不返回带有锁定单元格的公式