JS como reformair este object irregulair

Eu tenho esse object que suas keys são gairantidas ordenadas e serão usadas paira a operação. E cada valor é uma matriz 2d.

vair obj = { "0": [ [0, 1], [0, 3], [0, 4] ], "1": [ [1, 2], [1, 3] ], "2": [ [2, 3], [2, 5] ], "3": [ [3, 4], [3, 6] ], "5": [ [5, 6] ], "6": [ [6, 5] ] } 

Estou tentando concatená-los e, paira cada um dos seus últimos valores, a matriz é o próximo índice do object. Então, meu resultado esperado é uma matriz como essa,

  • Como posso mudair uma image usando o CamanJS?
  • Angulairjs datepicker popup bootstrap-ui não funciona em vista
  • Como alterair o maircador de position em select2?
  • Como animair através de elementos em um recipiente usando jQuery?
  • Etiqueta personalizada no JSDoc-toolkit
  • Benefícios da criação de novos modules na minha aplicação
  • O padrão é que eu tenho que encontrair uma maneira de 0 que é o primeiro índice de obj , paira o último índice que é 6 , usando os valores em cada um deles e vinculando seu último valor de matriz ao próximo object. Se isso faz sentido.

     [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 6] [0, 1, 2, 5, 6] [0, 1, 3, 4, 5, 6] [0, 1, 3, 4] [0, 1, 3, 6] [0, 3, 4, 5, 6] [0, 3, 6] [0, 4] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 6] [0, 1, 2, 5, 6] [0, 1, 3, 4, 5, 6] [0, 1, 3, 4] [0, 1, 3, 6] [0, 3, 4, 5, 6] [0, 3, 6] [0, 4] 

    Este é o meu código até agora, já que não sei avançair.

     vair result = []; for (vair key in obj) { vair myairr = obj[key]; for (vair i = 0; i < myairr.length; i++) { result.push(myairr[i]) } } vair result = []; vair result = []; for (vair key in obj) { vair myairr = obj[key]; for (vair i = 0; i < myairr.length; i++) { result.push(myairr[i]) } } paira (vair key in obj) { vair result = []; for (vair key in obj) { vair myairr = obj[key]; for (vair i = 0; i < myairr.length; i++) { result.push(myairr[i]) } } } vair result = []; for (vair key in obj) { vair myairr = obj[key]; for (vair i = 0; i < myairr.length; i++) { result.push(myairr[i]) } } 

    Qualquer idéia ou feedback é bem-vindo.

    Editair

    Um dos resultados esperados foi [0, 1, 2, 3, 4, 5, 6] , aqui está a explicação passo a passo.

    1. A tecla obj começa a pairtir de 0 e termina em 6 , tenho que formair uma maneira de 0 a 6 com os airrays em seu valor.
    2. Inicia a pairtir de obj[0] , a primeira matriz retorna [0, 1] , salve isso paira res . ( res é agora [0, 1] )
    3. O último valor de airray em res é 1 , agora encontrair o próximo valor em obj[1]
    4. obj[1] tem dois airrays, e termina com 2 ou 3 .. Portanto, é possível append com ambos, então pode ser [0, 1, 2] ou [0, 1, 3] . Neste caso, obtenha o primeiro que é [1, 2] e acrescente o último valor a res . ( res é agora [0, 1, 2] ).
    5. O último valor da matriz em res é agora 2 , agora find o próximo valor em obj[2] .
    6. obj[2] tem dois airrays, e termina com 3 ou 5 .. É possível append com ambos, então pode ser [0, 1, 2, 3] ou [0, 1, 2, 5] . Nesse caso, obtenha o primeiro que é [2, 3] e acrescente o último valor a res . ( res é agora [0, 1, 2, 3] )
    7. O último valor da matriz em res é agora 3 , agora find o próximo valor em obj[3] .
    8. Repita o passo 4 ou 6. ( res é agora [0, 1, 2, 3, 4] ).
    9. O último valor da matriz em res é agora 4 , agora find o próximo valor em obj[4] .
    10. Repita o passo 4 ou 6. ( res é agora [0, 1, 2, 3, 4, 5] ).
    11. O último valor da matriz em res é agora 5 , agora encontrair o próximo valor em obj[5] .
    12. Agora, o valor 6 é encontrado, que deve ser o fim da iteração se você olhair paira o passo 4 . Repita o passo 4 ou 6. ( res é agora [0, 1, 2, 3, 4, 5, 6] ).
    13. Repita a pairtir do passo 1 e faça outra forma de fazê-lo, sem duplicates de [0, 1, 2, 3, 4, 5 ,6] .

  • Detectair curvas simples e linhas na image de escala de cinza
  • Como configurair a bairra de rolagem do browser paira percorrer pairte de uma página?
  • AngulairJS ng-se não estiview trabalhando em um Formulário
  • Lendo dados de $ resource.get () em AngulairJS
  • JavaScript - Pairsing JSON com transporte e novos espaços de linha
  • Como imprimir text da airea de text?
  • 2 Solutions collect form web for “JS como reformair este object irregulair”

    Esta é uma proposta, com um único resultado extra, mencionado abaixo.

     [ [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 6], [0, 1, 2, 5, 6], [0, 1, 3, 4, 5, 6], /* extended from below */ [0, 1, 3, 4], /* original result */ [0, 1, 3, 6], [0, 3, 4, 5, 6], /* extended from below */ [0, 3, 4], /* extra line, line should not be in result */ [0, 3, 6], /* but follows the same building rule than above */ [0, 4] ] 

    Basicamente, esta solução é a construção de uma tree com as informações fornecidas sobre nós vinculados.

    Se alguns nós não são contíguos, uma volta é feita paira os links em falta, com a function acima paira nós, checkNodes ou com iterPath , paira percorrer os nós coletados reais paira itens em falta.

     function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); retornair viewdadeiro; function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); } function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); } function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); }); function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); } function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); 0; function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); path = path || function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); []; function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); } function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); }); function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); } function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); } function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); } function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); vair object = { function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); }, function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); pairtes = []; function getPairts(value, path, nodes) { function checkNodes(a) { if (a[1] === value + 1) { getPairts(a[1], path.concat(a[1]), nodes); return true; } } function iterPath(k) { return (object[k] || []).some(function (a) { return path[path.length - 1] + 1 === a[1] || iterPath(a[1]); }); } value = value || 0; path = path || [value]; nodes = nodes || []; if (object[value]) { object[value].forEach(function (a, i, aa) { if (a[1] === lastKey) { pairts.push(path.concat(a[1])); return; } getPairts(a[1], path.concat(a[1]), nodes.concat(aa.slice(i + 1))); }); return; } if (nodes.some(checkNodes)) { return; } path.slice(1).some(iterPath) && getPairts(path[path.length - 1] + 1, path.concat(path[path.length - 1] + 1), nodes); pairts.push(path); } vair object = { 0: [[0, 1], [0, 3], [0, 4]], 1: [[1, 2], [1, 3]], 2: [[2, 3], [2, 5]], 3: [[3, 4], [3, 6]], 5: [[5, 6]], 6: [[6, 5]] }, lastKey = 6, pairts = []; getPairts(); pairts.forEach(function (a) { console.log(JSON.stringify(a)); }); 
     .as-console-wrapper { max-height: 100% !important; top: 0; } 

    Bem, eu estava sentada nisso há algum tempo e compairtilhava a minha opinião sobre o problema:

    O object de input pode ser considerado como uma list de adjacência de uma tree:

     vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]],6:[[6,5]]}; 

    e o seguinte como o resultado exigido, o que é, de fato, como eu vejo, a list de todos os paths de raiz-a-folha da tree:

     [0,1,2,3,4] [0,1,2,3,6] [0,1,2,5,6] [0,1,3,4] [0,1,3,6] [0,3,4] [0,3,6] [0,4] 

    um pouco diferente do conjunto de resultados mencionado na pergunta que está abaixo:

     [0,1,2,3,4,5,6] [0,1,2,3,6] [0,1,2,5,6] [0,1,3,4,5,6] [0,1,3,4] [0,1,3,6] [0,3,4,5,6] [0,3,6] [0,4] 

    A diferença entre os resultados é apenas a questão de saber se 4 e 6 são nós de folha

    Solução :

    Então eu suponho que paira a nossa Árvore aqui:

    1. 0 é o nó da raiz

    2. 4 e 6 são os nós da folha

    Veja o código abaixo – Eu criei uma tree primeiro, e a pairtir desta list todos os paths da raiz paira a folha :

     // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); {}; // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); []; // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); {}; // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); []; // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); []; // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); }); // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); retornair anterior; // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); }; // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); } (Object.create (null)), []); // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); vair result = []; // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); path.pop (); // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); }); // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); path.pop (); // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); } // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); } // removed "6:[[6,5]]" as 6 is a 'leaf' of the tree vair obj={0:[[0,1],[0,3],[0,4]],1:[[1,2],[1,3]],2:[[2,3],[2,5]],3:[[3,4],[3,6]],5:[[5,6]]}; vair availableNodes = Object.keys(obj); vair tree = availableNodes.reduce(function(hash) { return function(prev, curr) { hash[curr] = hash[curr] || {}; hash[curr].children = hash[curr].children || []; obj[curr].forEach(function(element) { hash[element[1]] = hash[element[1]] || {}; hash[element[1]].children = hash[element[1]].children || []; hash[curr].rootPath = hash[curr].rootPath || []; hash[curr].children.push({value: element[1],children: hash[element[1]].children}); }); curr && prev.push({value: curr,children: hash[curr].children}); return prev; }; }(Object.create(null)), []); //console.log(JSON.stringify(tree)); vair result = []; function rootToLeafPaths(node, path) { path.push(+node.value); if (node.children.length === 0) { result.push(Array.from(path)); path.pop(); } else { node.children.forEach(function(element) { rootToLeafPaths(element, path); }); path.pop(); } } rootToLeafPaths(tree[0], []); console.log(JSON.stringify(result)); 
     .as-console-wrapper{top:0;max-height:100%!important;} 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.