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

163
Visualizações
Node.js/Javascript Recursive Map Reduction to remove unchanged children

I have a key/value map of paths that have changed that looks like this example:

{
  '/A' => '/AA',
  '/A/B' => '/AA/B',
  '/A/C' => '/AA/C',
  '/A/D' => '/AA/D'
}

The left value is the old path, the right value is the new path.

I need to reduce this to just the paths that have changed and filter out the redundant non-changed children. For example, if I change '/A' to '/AA' then I don't need the children, unless they have been changed, etc.

The rub is these paths can be quite deep and I need to somehow recursively end up with just what has actually changed.

3 days no luck, can't get my head around it, thanks mighty code warriors for any help :)

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

0

Just iterate them and remove everything for which a matching ancestor move exists:

const moves = new Map([
  ['/A', '/AA'],
  ['/A/B', '/AA/B'],
  ['/A/C', '/AA/C'],
  ['/A/D', '/AA/X'],
]);

for (const [from, to] of moves) {
  const fromParts = from.split('/');
  for (let i=1; i<fromParts.length; i++) {
    const parentFrom = fromParts.slice(0, i).join('/');
    const parentTo = moves.get(parentFrom);
    if (parentTo !== undefined) {
      const sub = '/' + fromParts.slice(i).join('/');
      if (parentTo + sub === to) {
        console.log(`Move ${from}=>${to} contained in move ${parentFrom}=>${parentTo}`);
        moves.delete(from);
      } else {
        console.log(`Source ${from} is part of ${parentFrom}, but ${sub} was moved to ${to} instead of ${parentTo + sub}`);
      }
    }
  }
}
console.log(Object.fromEntries(moves));

If you don't like to mutate the input moves map, you can also just use filter() on an array of moves; it just needs to be able to lookup potential parent moves.

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