数组组合为数组

我有以下数组:


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": []

    }

  ]

];

我使用以下函数给出三个数组的组合:


const combinations = data.reduce((a, b) => {

    return a.reduce((r, v) => {

    return r.concat(b.map(w => {

        return [].concat(v, w);

    }));

  }, []);

});


浮云间
浏览 150回答 2
2回答

胡说叔叔

您需要过滤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&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; 3asia&nbsp; &nbsp; india&nbsp; &nbsp; digitaleurope&nbsp; italy&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; malaysia&nbsp;独特的组合:1&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; 3asia&nbsp; &nbsp; india&nbsp; &nbsp; digitalasia&nbsp; &nbsp; italy&nbsp; &nbsp; digitalasia&nbsp; &nbsp; malaysia digitaleurope&nbsp; india&nbsp; &nbsp; digitaleurope&nbsp; italy&nbsp; &nbsp; digital您已经展示了如何使用数组函数来执行此操作。通过递归,可以实现相同的效果,如下所示:https ://jsfiddle.net/vahz3n64/function getCombinationsByOrder() {&nbsp; &nbsp;let recur = (iarray) => {&nbsp; &nbsp; &nbsp; var a = [];&nbsp; &nbsp; &nbsp; var temp = [];&nbsp; &nbsp; &nbsp; if(iarray === data.length-1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (data[iarray]);&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp = recur(iarray + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var i=0;i<data[iarray].length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var j=0;j<temp.length;j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.push([data[iarray][i], temp[j]].flat());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a;&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp;};&nbsp; &nbsp;return recur(0);};const data = [&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02639a62a612a172d9d0",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb",&nbsp; &nbsp; &nbsp; "name": "asia",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172db2f",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb",&nbsp; &nbsp; &nbsp; "name": "europe",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; }&nbsp; ],&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02639a62a612a172d9d5",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "india",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172d9d0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172da4a"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172dba0",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "italy",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02649a62a612a172db2f"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02669a62a612a172e394"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172dd26",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "malaysia",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172d9d0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02649a62a612a172dd23"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; ],&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f7c8a000e746271f08f95cf",&nbsp; &nbsp; &nbsp; "level": "5f22c4d180556e32cf0ca8ff",&nbsp; &nbsp; &nbsp; "name": "digital",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; }&nbsp; ]];console.log(getCombinationsByOrder());按级别这是按标准进行组合。当按级别过滤时,如果在下一个子数组中的元素的数组level中找到给定元素的值,则假定包含匹配项。relations编辑:如果relations数组为空,则也包含该元素。例如,asia第一个子数组的级别为5f22c4af80556e32cf0ca8fb。因此,india应该包含下一个子数组,因为它的relations数组中有一个包含该级别的条目。这似乎并不是唯一的标准,但无论如何,结果将是:1&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; 3asia&nbsp; &nbsp; india&nbsp; &nbsp; digitalasia&nbsp; &nbsp; italy&nbsp; &nbsp; digitalasia&nbsp; &nbsp; malaysia digitaleurope&nbsp; india&nbsp; &nbsp; digitaleurope&nbsp; italy&nbsp; &nbsp; digitaleurope&nbsp; malaysia digital这种过滤可以这样实现: https: //jsfiddle.net/35kqbp18/function getCombinationsByLevel() {&nbsp; &nbsp;let recur = (iarray, level) => {&nbsp; &nbsp; &nbsp; var a = [];&nbsp; &nbsp; &nbsp; var temp = [];&nbsp; &nbsp; &nbsp; var arr = [];&nbsp; &nbsp; &nbsp; if(level !== null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var i=0;i<data[iarray].length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(checkHasLevel(data[iarray][i], level)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr.push(data[iarray][i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr = data[iarray];&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; if(iarray === data.length-1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (arr);&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var i=0;i<arr.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = recur(iarray + 1, arr[i].level);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(temp.length !== 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var j=0;j<temp.length;j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.push([arr[i], temp[j]].flat()); //parsing to prevent circular object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.push(arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a;&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp;};&nbsp; &nbsp;let checkHasLevel = (obj, level) => {&nbsp; &nbsp; &nbsp; if(obj.relations.length === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true;&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; for(var i=0;i<obj.relations.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(obj.relations[i].level === level) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp;};&nbsp; &nbsp;return recur(0, null);};const data = [&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02639a62a612a172d9d0",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb",&nbsp; &nbsp; &nbsp; "name": "asia",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172db2f",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb",&nbsp; &nbsp; &nbsp; "name": "europe",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; }&nbsp; ],&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02639a62a612a172d9d5",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "india",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172d9d0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172da4a"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172dba0",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "italy",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02649a62a612a172db2f"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02669a62a612a172e394"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172dd26",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "malaysia",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172d9d0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02649a62a612a172dd23"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; ],&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f7c8a000e746271f08f95cf",&nbsp; &nbsp; &nbsp; "level": "5f22c4d180556e32cf0ca8ff",&nbsp; &nbsp; &nbsp; "name": "digital",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; }&nbsp; ]];console.log(getCombinationsByLevel());通过ID这遵循与按级别过滤相同的逻辑,但使用一个元素的值并在下一个子数组中元素的数组(在 内)_id中搜索它。编辑:如果数组为空,则也包含该元素。relationsitemsrelations例如,asia第一个子数组的 ID 为5f6a02639a62a612a172d9d0。因此,india应该包含下一个子数组,因为它的relations数组中有一个带有该 ID 的条目。其结果将是:1&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; 3asia&nbsp; &nbsp; india&nbsp; &nbsp; digitalasia&nbsp; &nbsp; malaysia digitaleurope&nbsp; italy&nbsp; &nbsp; digital这种过滤可以这样实现: https: //jsfiddle.net/5apx7foq/function getCombinationsById() {&nbsp; &nbsp;let recur = (iarray, id) => {&nbsp; &nbsp; &nbsp; var a = [];&nbsp; &nbsp; &nbsp; var temp = [];&nbsp; &nbsp; &nbsp; var arr = [];&nbsp; &nbsp; &nbsp; if(id !== null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var i=0;i<data[iarray].length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(checkHasId(data[iarray][i], id)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr.push(data[iarray][i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr = data[iarray];&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; if(iarray === data.length-1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (arr);&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var i=0;i<arr.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = recur(iarray + 1, arr[i]._id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(temp.length !== 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var j=0;j<temp.length;j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.push([arr[i], temp[j]].flat());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.push(arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a;&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp;};&nbsp; &nbsp;let checkHasId = (obj, id) => {&nbsp; &nbsp; &nbsp; if(obj.relations.length === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true;&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; for(var i=0;i<obj.relations.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(obj.relations[i].items.indexOf(id) !== -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp;};&nbsp; &nbsp;return recur(0, null);};const data = [&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02639a62a612a172d9d0",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb",&nbsp; &nbsp; &nbsp; "name": "asia",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172db2f",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb",&nbsp; &nbsp; &nbsp; "name": "europe",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; }&nbsp; ],&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02639a62a612a172d9d5",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "india",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172d9d0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172da4a"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172dba0",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "italy",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02649a62a612a172db2f"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02669a62a612a172e394"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172dd26",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "malaysia",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172d9d0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02649a62a612a172dd23"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; ],&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f7c8a000e746271f08f95cf",&nbsp; &nbsp; &nbsp; "level": "5f22c4d180556e32cf0ca8ff",&nbsp; &nbsp; &nbsp; "name": "digital",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; }&nbsp; ]];console.log(getCombinationsById());组合标准假设这data可能只是更多数据的样本,可能有更多的变化,这里有一个组合函数,它可以根据使用的选项返回上述所有结果的结果。它还可以返回按 ID 和级别、或按 ID 或级别过滤的结果。https://jsfiddle.net/xmfjh8s9/function getCombinationsByMatch(matchId, matchLevel, matchAnd) {&nbsp; &nbsp;let recur = (iarray, id, level) => {&nbsp; &nbsp; &nbsp; var a = [];&nbsp; &nbsp; &nbsp; var temp = [];&nbsp; &nbsp; &nbsp; var arr = [];&nbsp; &nbsp; &nbsp; if(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(matchId && (id !== null)) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(matchLevel && (level !== null))&nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var i=0;i<data[iarray].length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(checkMatch(data[iarray][i], id, level)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr.push(data[iarray][i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr = data[iarray];&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; if(iarray === data.length-1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (arr);&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var i=0;i<arr.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = recur(iarray + 1, arr[i]._id, arr[i].level);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(temp.length !== 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(var j=0;j<temp.length;j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.push([arr[i], temp[j]].flat());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.push(arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a;&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp;};&nbsp; &nbsp;let checkMatch = (obj, id, level) => {&nbsp; &nbsp; &nbsp; if(obj.relations.length === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true;&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; for(var i=0;i<obj.relations.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;matchAnd && matchId && matchLevel &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(obj.relations[i].items.indexOf(id) !== -1) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(obj.relations[i].level === level)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;!matchAnd && matchId && matchLevel &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (obj.relations[i].items.indexOf(id) !== -1) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (obj.relations[i].level === level)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;matchId && !matchLevel &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(obj.relations[i].items.indexOf(id) !== -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;!matchId && matchLevel &&&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(obj.relations[i].level === level)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp;};&nbsp; &nbsp;return recur(0, null, null);};const data = [&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02639a62a612a172d9d0",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb",&nbsp; &nbsp; &nbsp; "name": "asia",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172db2f",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb",&nbsp; &nbsp; &nbsp; "name": "europe",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; }&nbsp; ],&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02639a62a612a172d9d5",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "india",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172d9d0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172da4a"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172dba0",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "italy",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02649a62a612a172db2f"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02669a62a612a172e394"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f6a02649a62a612a172dd26",&nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fd",&nbsp; &nbsp; &nbsp; "name": "malaysia",&nbsp; &nbsp; &nbsp; "relations": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02639a62a612a172d9d0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fb"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "items": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5f6a02649a62a612a172dd23"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "level": "5f22c4af80556e32cf0ca8fc"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; ],&nbsp; [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "_id": "5f7c8a000e746271f08f95cf",&nbsp; &nbsp; &nbsp; "level": "5f22c4d180556e32cf0ca8ff",&nbsp; &nbsp; &nbsp; "name": "digital",&nbsp; &nbsp; &nbsp; "relations": []&nbsp; &nbsp; }&nbsp; ]];console.log(getCombinationsByMatch(true, true, true));这应该可以让您更灵活地在 中找到不同的组合data。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript