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

267
Visualizações
How to modify an array by link inside function?

I have the following function:

  public recursiveFilterLayers(node: LayerNode[]): LayerNode[] {
    const validItem = node.filter(
      (i) => i.visible === true || i.visible === undefined
    );

    validItem.forEach((i) => {
      if (i.children) i.children = this.recursiveFilterLayers(i.children);
    });

    return validItem;
  }

And using:

data = recursiveFilterLayers(data);

How to rewrite this function to use without return like:

recursiveFilterLayers(data);

I mean pass the reference to data array.

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

0

Array.filter returns a copy, so you can't mutate the original array with it. Instead, you will need to explicitly iterate over the array using a loop.

It's tempting to use splice to remove individual values, but this has O(n²) behaviour because splice has to copy all subsequent elements one place backwards every time an element is removed.

A better approach is to keep a read and a write index, and perform a single splice at the end to cut off the end of the array:

function recursiveFilterLayers(node) {
  let readIndex;
  let writeIndex = 0;
  for (readIndex = 0; readIndex < node.length; readIndex++) {
    const element = node[readIndex];
    if (element.visible === true || element.visible === undefined) {
      node[writeIndex] = element;
      if (element.children) {
        recursiveFilterLayers(element.children);
      }
      writeIndex++;
    }
  }

  node.splice(writeIndex, readIndex - writeIndex);
}

const data = [
  {visible: true},
  {},
  {visible: false},
  {
    children: [{visible: false}, {visible: true}],
  },
];
recursiveFilterLayers(data);
console.log(data);

about 4 years ago · Juan Pablo Isaza Relatório

0

In javascript, primitives are passed by value, and Objects are passed by "copy of a reference". You don't have to return at all, a function can modify the content.

Although you used a filter that copies the parameter's content thus won't modify anything. Instead, we can use forEach like following.

public recursiveFilterLayers(nodes: LayerNode[]): LayerNode[] {
    nodes.forEach(n=>{
        if((n.visible === true || n.visible === undefined) && n.children){
            this.recursiveFilterLayers(n.children);
        }
    });
    return;
}
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