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

125
Visualizações
Divide S3 prefix to list of object in Javascript

Example, I have a path(prefix) like this: A/B/C/

I want to get bellow list:

[{name:"A",path:"A/"},
 {name:"B",path:"A/B/",
 {name:"C",path:"A/B/C/"
]

I can split the path to a array, then loop the array to build the new list of object. But in my mind, I just know there should be a simple and smarter way to achieve this by using reducer, but just stuck here.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You're right that you could use a reducer. Something like this:

const str = "A/B/C/"

const arr = str.split("/").filter(Boolean).reduce((acc, name) => {
  const path = [...acc.map(o => o.name), name].join("/") + "/"
  return [...acc, { name, path }]
}, [])

console.log(arr)

about 4 years ago · Juan Pablo Isaza Relatório

0

My odd solution, but I think sdgluck's is the cleanest answer.

arr = "A/B/C/"
    .split("/")
    .filter(e => e)
    .map((e, i, a) => {
        a2 = a.filter((el, ix) => {
            if (ix <= i) return el;
        });

        return  {[e] : a2.join("/")};
    });
about 4 years ago · Juan Pablo Isaza Relatório

0

You can solve this with map, but maybe not as cleanly as you were anticipating:

const result = 'A/B/C'
  .split('/')
  .filter(x => x)
  .map((name, i, arr) => {
    const prev = arr[i - 1];
    return prev
      ? { name, path: `${prev.name}${name}/` }
      : { name, path: `${name}/` };
  });
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