Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
¿Cómo modificar una matriz por enlace dentro de la función?

tengo la siguiente función:

 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; }

Y usando:

 data = recursiveFilterLayers(data);

Cómo reescribir esta función para usar sin retorno como:

 recursiveFilterLayers(data);

Me refiero a pasar la referencia a la matriz de data .

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Array.filter devuelve una copia, por lo que no puede mutar la matriz original con ella. En su lugar, deberá iterar explícitamente sobre la matriz mediante un bucle.

Es tentador usar splice para eliminar valores individuales, pero esto tiene un comportamiento O(n²) porque el splice tiene que copiar todos los elementos subsiguientes un lugar hacia atrás cada vez que se elimina un elemento.

Un mejor enfoque es mantener un índice de lectura y escritura, y realizar un solo splice al final para cortar el final de la matriz:

 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 Report

0

En javascript, las primitivas se pasan por valor y los objetos se pasan por "copia de una referencia". No tiene que regresar en absoluto, una función puede modificar el contenido.

Aunque usó un filtro que copia el contenido del parámetro, por lo tanto, no modificará nada. En su lugar, podemos usar forEach como sigue.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!