Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Merge arbitrary amount of arrays, which may have shared elements, into a single, nested dictionary object that considers those shared elements?

I'm attempting to compile a big-picture "folder structure" dictionary object from multiple arrays that each contain folder name strings. Some of the folder/sub-folder names will be shared between the arrays and the nested dictionary object needs to reflect that.

When looping through each array of folder names to check if that "structure" already exists within the nested dictionary object, I can't seem to figure out how to account for previous folders and shared folders, considering there could be any number of folders in each array. How can you account for that arbitrary number without manually writing an endless amount of for-loops?

Here's a basic example of what the arrays could look like:

    var test1 = ["sub1", "sub2", "sub3"];
    var test2 = ["sub1", "other"];
    var test3 = ["sub0", "misc"];
    var test4 = ["sub1", "sub2", "proj", "img"];

And here's the desired output based on the above example:

    var folder_structure = {
        "sub0": {
            "misc": {}
        },
        "sub1": {
            "sub2": {
                "sub3": {},
                "proj": {
                    "img": {}
                }
            },
            "other": {}
        },
    };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Well, you can do that in many ways. Here's my solution based on recursion:

var test1 = ["sub1", "sub2", "sub3"];
var test2 = ["sub1", "other"];
var test3 = ["sub0", "misc"];
var test4 = ["sub1", "sub2", "proj", "img"];
const arrays = [test1, test2, test3, test4]; // just organizing all together

const addFolders = (root, folders) => {

    // let the base case for recursion be an empty folders array
    if (!folders.length) return root;

    const curFolderName = folders[0];
    const newFolder = root[curFolderName] || {};
    root[curFolderName] = newFolder;
    
    // recursive call:
    return addFolders(root[curFolderName], folders.slice(1));
}

// here you can also use an empty object as temp 
// result-storage and a simple for or forEach loop 
// instead of reduce method (up to your taste). 
const result = arrays.reduce((acc, folders) => addFolders(acc, folders) && acc, {}) 

console.dir(result);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda