如果在这种情况下在任何数组隐藏元素中找不到数组中的任何choice
对象,并且在这 3 个结果中的任何一个中都不可用,那么我需要隐藏它。我该怎么做?choices
results
choice
{"choice": "f"}
{"choice": "g"}
{"choices":[
{"choice": "a"},
{"choice": "b"},
{"choice": "c"},
{"choice": "d"},
{"choice": "e"},
{"choice": "f"},
{"choice": "g"}
],
"results":[
{"result":["a", "b", "c"]},
{"result":["a", "b", "d"]},
{"result":["a", "b", "e"]},
您可以从结果和过滤器选项中收集所有值。
var object = { choices: [{ choice: "a" }, { choice: "b" }, { choice: "c" }, { choice: "d" }, { choice: "e" }, { choice: "f" }, { choice: "g" }], results: [{ result: ["a", "b", "c"] }, { result: ["a", "b", "d"] }, { result: ["a", "b", "e"] }] },
hash = Object.create(null),
notIn = [];
object.results.forEach(function (a) {
a.result.forEach(function (b) {
hash[b] = true;
});
});
notIn = object.choices.filter(function (a) {
return !hash[a.choice];
});
console.log(notIn);
.as-console-wrapper { max-height: 100% !important; top: 0; }
ES6 与 Set
var object = { choices: [{ choice: "a" }, { choice: "b" }, { choice: "c" }, { choice: "d" }, { choice: "e" }, { choice: "f" }, { choice: "g" }], results: [{ result: ["a", "b", "c"] }, { result: ["a", "b", "d"] }, { result: ["a", "b", "e"] }] },
results = object.results.reduce((s, a) => new Set([...s, ...a.result]), new Set),
notIn = object.choices.filter(a => !results.has(a.choice));
console.log(notIn);
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句