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

144
Views
Cómo manipular una matriz de objetos sin bucle

Tengo un código como el siguiente y funciona bien.

 for(let i=0;i< this.Array.length ; i++){ if(this.Array[i].propertyObject.hasOwnProperty('header')) this.Array[i].ColumnName = this.Array[i].propertyObject.header; }

¿Puedo saber cómo lograr lo mismo con Map. Gracias por adelantado.

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

0

¿Puedo saber cómo lograr lo mismo con Map?

Supongo que te refieres al map . map no es la herramienta adecuada para hacer exactamente lo que hace ese ciclo, porque ese ciclo modifica la matriz en su lugar, pero map crea una nueva matriz en su lugar.

Si desea una nueva matriz , quizás también con nuevos objetos (por ejemplo, programación funcional o programación inmutable):

 // Replace `this.Array` with a new array this.Array = this.Array.map(element => { // If we need to change this element... if (element.propertyObject.hasOwnProperty("header")) { // ...do a shallow copy along with the replacement element = {...element, ColumnName: element.propertyObject.header}; } return element; });

Tenga en cuenta que eso supone que los elementos son objetos simples. Si no lo son, deberá manejar la construcción del reemplazo de manera diferente a simplemente usar {...original} .

Pero si desea mantener la misma matriz que su código actual , su ciclo está bien. Tienes otras opciones (como forEach o for-of ), pero lo que tienes también está bien. for-of se adapta bien a lo que estás haciendo:

 for (const element of this.Array) { if (element.propertyObject.hasOwnProperty("header")) { element.ColumnName = element.propertyObject.header; } }

Nota al margen: en el código nuevo, es posible que desee usar Object.hasOwn en lugar de Object.prototype.hasOwnProperty (con un relleno poligonal si es necesario para entornos más antiguos; sin embargo, las versiones recientes de todos los navegadores modernos lo admiten de forma nativa).

about 4 years ago · Juan Pablo Isaza Report

0

this.Array.forEach((item, index, arr)=> { item.ColumnName = "header" in item ? item.header : item.ColumnName; });
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!