胡说叔叔
您需要过滤b,而不是返回一个空数组,因为空数组并不排除实际用作结果的集合,而只是排除v和w对象并包含以下数组中的对象。const data = [[{ _id: "5f6a02639a62a612a172d9d0", level: "5f22c4af80556e32cf0ca8fb", name: "asia", relations: [] }, { _id: "5f6a02649a62a612a172db2f", level: "5f22c4af80556e32cf0ca8fb", name: "europe", relations: [] }], [{ _id: "5f6a02639a62a612a172d9d5", level: "5f22c4af80556e32cf0ca8fd", name: "india", relations: [{ items: ["5f6a02639a62a612a172d9d0"], level: "5f22c4af80556e32cf0ca8fb" }, { items: ["5f6a02639a62a612a172da4a"], level: "5f22c4af80556e32cf0ca8fc" }] }, { _id: "5f6a02649a62a612a172dba0", level: "5f22c4af80556e32cf0ca8fd", name: "italy", relations: [{ items: ["5f6a02649a62a612a172db2f"], level: "5f22c4af80556e32cf0ca8fb" }, { items: ["5f6a02669a62a612a172e394"], level: "5f22c4af80556e32cf0ca8fc" }] }, { _id: "5f6a02649a62a612a172dd26", level: "5f22c4af80556e32cf0ca8fd", name: "malaysia", relations: [{ items: ["5f6a02639a62a612a172d9d0"], level: "5f22c4af80556e32cf0ca8fb" }, { items: ["5f6a02649a62a612a172dd23"], level: "5f22c4af80556e32cf0ca8fc" }] }], [{ _id: "5f7c8a000e746271f08f95cf", level: "5f22c4d180556e32cf0ca8ff", name: "digital", relations: [] }]], combinations = data.reduce((a, b) => { return a.reduce((r, v) => { return r.concat(b .filter(({ relations }) => !relations.length || relations.some(({ items, level }) => level === v.level && items[0] === v._id) ) .map(w => [].concat(v, w)) ); }, []); });console.log(combinations);.as-console-wrapper { max-height: 100% !important; top: 0; }
holdtom
从递归的角度而不是从许多嵌套的数组函数的角度来思考这个问题似乎更清楚。按命令最简单的情况是给出 3 个子数组中的每种可能的组合。子数组:1 2 3asia india digitaleurope italy malaysia 独特的组合:1 2 3asia india digitalasia italy digitalasia malaysia digitaleurope india digitaleurope italy digital您已经展示了如何使用数组函数来执行此操作。通过递归,可以实现相同的效果,如下所示:https ://jsfiddle.net/vahz3n64/function getCombinationsByOrder() { let recur = (iarray) => { var a = []; var temp = []; if(iarray === data.length-1) { return (data[iarray]); } else { temp = recur(iarray + 1); for(var i=0;i<data[iarray].length;i++) { for(var j=0;j<temp.length;j++) { a.push([data[iarray][i], temp[j]].flat()); }; }; return a; }; }; return recur(0);};const data = [ [ { "_id": "5f6a02639a62a612a172d9d0", "level": "5f22c4af80556e32cf0ca8fb", "name": "asia", "relations": [] }, { "_id": "5f6a02649a62a612a172db2f", "level": "5f22c4af80556e32cf0ca8fb", "name": "europe", "relations": [] } ], [ { "_id": "5f6a02639a62a612a172d9d5", "level": "5f22c4af80556e32cf0ca8fd", "name": "india", "relations": [ { "items": [ "5f6a02639a62a612a172d9d0" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02639a62a612a172da4a" ], "level": "5f22c4af80556e32cf0ca8fc" } ] }, { "_id": "5f6a02649a62a612a172dba0", "level": "5f22c4af80556e32cf0ca8fd", "name": "italy", "relations": [ { "items": [ "5f6a02649a62a612a172db2f" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02669a62a612a172e394" ], "level": "5f22c4af80556e32cf0ca8fc" } ] }, { "_id": "5f6a02649a62a612a172dd26", "level": "5f22c4af80556e32cf0ca8fd", "name": "malaysia", "relations": [ { "items": [ "5f6a02639a62a612a172d9d0" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02649a62a612a172dd23" ], "level": "5f22c4af80556e32cf0ca8fc" } ] } ], [ { "_id": "5f7c8a000e746271f08f95cf", "level": "5f22c4d180556e32cf0ca8ff", "name": "digital", "relations": [] } ]];console.log(getCombinationsByOrder());按级别这是按标准进行组合。当按级别过滤时,如果在下一个子数组中的元素的数组level中找到给定元素的值,则假定包含匹配项。relations编辑:如果relations数组为空,则也包含该元素。例如,asia第一个子数组的级别为5f22c4af80556e32cf0ca8fb。因此,india应该包含下一个子数组,因为它的relations数组中有一个包含该级别的条目。这似乎并不是唯一的标准,但无论如何,结果将是:1 2 3asia india digitalasia italy digitalasia malaysia digitaleurope india digitaleurope italy digitaleurope malaysia digital这种过滤可以这样实现: https: //jsfiddle.net/35kqbp18/function getCombinationsByLevel() { let recur = (iarray, level) => { var a = []; var temp = []; var arr = []; if(level !== null) { for(var i=0;i<data[iarray].length;i++) { if(checkHasLevel(data[iarray][i], level)) { arr.push(data[iarray][i]); }; }; } else { arr = data[iarray]; }; if(iarray === data.length-1) { return (arr); } else { for(var i=0;i<arr.length;i++) { temp = recur(iarray + 1, arr[i].level); if(temp.length !== 0) { for(var j=0;j<temp.length;j++) { a.push([arr[i], temp[j]].flat()); //parsing to prevent circular object }; } else { a.push(arr[i]); } }; return a; }; }; let checkHasLevel = (obj, level) => { if(obj.relations.length === 0) { return true; }; for(var i=0;i<obj.relations.length;i++) { if(obj.relations[i].level === level) { return true; }; }; return false; }; return recur(0, null);};const data = [ [ { "_id": "5f6a02639a62a612a172d9d0", "level": "5f22c4af80556e32cf0ca8fb", "name": "asia", "relations": [] }, { "_id": "5f6a02649a62a612a172db2f", "level": "5f22c4af80556e32cf0ca8fb", "name": "europe", "relations": [] } ], [ { "_id": "5f6a02639a62a612a172d9d5", "level": "5f22c4af80556e32cf0ca8fd", "name": "india", "relations": [ { "items": [ "5f6a02639a62a612a172d9d0" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02639a62a612a172da4a" ], "level": "5f22c4af80556e32cf0ca8fc" } ] }, { "_id": "5f6a02649a62a612a172dba0", "level": "5f22c4af80556e32cf0ca8fd", "name": "italy", "relations": [ { "items": [ "5f6a02649a62a612a172db2f" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02669a62a612a172e394" ], "level": "5f22c4af80556e32cf0ca8fc" } ] }, { "_id": "5f6a02649a62a612a172dd26", "level": "5f22c4af80556e32cf0ca8fd", "name": "malaysia", "relations": [ { "items": [ "5f6a02639a62a612a172d9d0" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02649a62a612a172dd23" ], "level": "5f22c4af80556e32cf0ca8fc" } ] } ], [ { "_id": "5f7c8a000e746271f08f95cf", "level": "5f22c4d180556e32cf0ca8ff", "name": "digital", "relations": [] } ]];console.log(getCombinationsByLevel());通过ID这遵循与按级别过滤相同的逻辑,但使用一个元素的值并在下一个子数组中元素的数组(在 内)_id中搜索它。编辑:如果数组为空,则也包含该元素。relationsitemsrelations例如,asia第一个子数组的 ID 为5f6a02639a62a612a172d9d0。因此,india应该包含下一个子数组,因为它的relations数组中有一个带有该 ID 的条目。其结果将是:1 2 3asia india digitalasia malaysia digitaleurope italy digital这种过滤可以这样实现: https: //jsfiddle.net/5apx7foq/function getCombinationsById() { let recur = (iarray, id) => { var a = []; var temp = []; var arr = []; if(id !== null) { for(var i=0;i<data[iarray].length;i++) { if(checkHasId(data[iarray][i], id)) { arr.push(data[iarray][i]); }; }; } else { arr = data[iarray]; }; if(iarray === data.length-1) { return (arr); } else { for(var i=0;i<arr.length;i++) { temp = recur(iarray + 1, arr[i]._id); if(temp.length !== 0) { for(var j=0;j<temp.length;j++) { a.push([arr[i], temp[j]].flat()); }; } else { a.push(arr[i]); } }; return a; }; }; let checkHasId = (obj, id) => { if(obj.relations.length === 0) { return true; }; for(var i=0;i<obj.relations.length;i++) { if(obj.relations[i].items.indexOf(id) !== -1) { return true; } }; return false; }; return recur(0, null);};const data = [ [ { "_id": "5f6a02639a62a612a172d9d0", "level": "5f22c4af80556e32cf0ca8fb", "name": "asia", "relations": [] }, { "_id": "5f6a02649a62a612a172db2f", "level": "5f22c4af80556e32cf0ca8fb", "name": "europe", "relations": [] } ], [ { "_id": "5f6a02639a62a612a172d9d5", "level": "5f22c4af80556e32cf0ca8fd", "name": "india", "relations": [ { "items": [ "5f6a02639a62a612a172d9d0" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02639a62a612a172da4a" ], "level": "5f22c4af80556e32cf0ca8fc" } ] }, { "_id": "5f6a02649a62a612a172dba0", "level": "5f22c4af80556e32cf0ca8fd", "name": "italy", "relations": [ { "items": [ "5f6a02649a62a612a172db2f" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02669a62a612a172e394" ], "level": "5f22c4af80556e32cf0ca8fc" } ] }, { "_id": "5f6a02649a62a612a172dd26", "level": "5f22c4af80556e32cf0ca8fd", "name": "malaysia", "relations": [ { "items": [ "5f6a02639a62a612a172d9d0" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02649a62a612a172dd23" ], "level": "5f22c4af80556e32cf0ca8fc" } ] } ], [ { "_id": "5f7c8a000e746271f08f95cf", "level": "5f22c4d180556e32cf0ca8ff", "name": "digital", "relations": [] } ]];console.log(getCombinationsById());组合标准假设这data可能只是更多数据的样本,可能有更多的变化,这里有一个组合函数,它可以根据使用的选项返回上述所有结果的结果。它还可以返回按 ID 和级别、或按 ID 或级别过滤的结果。https://jsfiddle.net/xmfjh8s9/function getCombinationsByMatch(matchId, matchLevel, matchAnd) { let recur = (iarray, id, level) => { var a = []; var temp = []; var arr = []; if( (matchId && (id !== null)) || (matchLevel && (level !== null)) ) { for(var i=0;i<data[iarray].length;i++) { if(checkMatch(data[iarray][i], id, level)) { arr.push(data[iarray][i]); }; }; } else { arr = data[iarray]; }; if(iarray === data.length-1) { return (arr); } else { for(var i=0;i<arr.length;i++) { temp = recur(iarray + 1, arr[i]._id, arr[i].level); if(temp.length !== 0) { for(var j=0;j<temp.length;j++) { a.push([arr[i], temp[j]].flat()); }; } else { a.push(arr[i]); } }; return a; }; }; let checkMatch = (obj, id, level) => { if(obj.relations.length === 0) { return true; }; for(var i=0;i<obj.relations.length;i++) { if( ( matchAnd && matchId && matchLevel && (obj.relations[i].items.indexOf(id) !== -1) && (obj.relations[i].level === level) ) || ( !matchAnd && matchId && matchLevel && ( (obj.relations[i].items.indexOf(id) !== -1) || (obj.relations[i].level === level) ) ) || ( matchId && !matchLevel && (obj.relations[i].items.indexOf(id) !== -1) ) || ( !matchId && matchLevel && (obj.relations[i].level === level) ) ) { return true; } }; return false; }; return recur(0, null, null);};const data = [ [ { "_id": "5f6a02639a62a612a172d9d0", "level": "5f22c4af80556e32cf0ca8fb", "name": "asia", "relations": [] }, { "_id": "5f6a02649a62a612a172db2f", "level": "5f22c4af80556e32cf0ca8fb", "name": "europe", "relations": [] } ], [ { "_id": "5f6a02639a62a612a172d9d5", "level": "5f22c4af80556e32cf0ca8fd", "name": "india", "relations": [ { "items": [ "5f6a02639a62a612a172d9d0" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02639a62a612a172da4a" ], "level": "5f22c4af80556e32cf0ca8fc" } ] }, { "_id": "5f6a02649a62a612a172dba0", "level": "5f22c4af80556e32cf0ca8fd", "name": "italy", "relations": [ { "items": [ "5f6a02649a62a612a172db2f" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02669a62a612a172e394" ], "level": "5f22c4af80556e32cf0ca8fc" } ] }, { "_id": "5f6a02649a62a612a172dd26", "level": "5f22c4af80556e32cf0ca8fd", "name": "malaysia", "relations": [ { "items": [ "5f6a02639a62a612a172d9d0" ], "level": "5f22c4af80556e32cf0ca8fb" }, { "items": [ "5f6a02649a62a612a172dd23" ], "level": "5f22c4af80556e32cf0ca8fc" } ] } ], [ { "_id": "5f7c8a000e746271f08f95cf", "level": "5f22c4d180556e32cf0ca8ff", "name": "digital", "relations": [] } ]];console.log(getCombinationsByMatch(true, true, true));这应该可以让您更灵活地在 中找到不同的组合data。