Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda