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

238
Views
Copia profunda de una matriz en Angular 2 + TypeScript

Tengo una matriz de objetos que es una entrada. Llamémoslo content .

Al intentar copiarlo en profundidad, todavía tiene una referencia a la matriz anterior.

Necesito duplicar esa matriz de entrada y cambiar una propiedad de la parte duplicada.

Tanto tiempo he intentado diferentes métodos que no tuvieron éxito.

ES6 manera:

 public duplicateArray() { arr = [...this.content] arr.map((x) => {x.status = DEFAULT}); return this.content.concat(arr); }

La forma slice :

 public duplicateArray() { arr = this.content.slice(0); arr.map((x) => {x.status = DEFAULT}); return this.content.concat(arr); }

En ambos, todos los objetos dentro de la matriz tienen el status: 'Default' .

¿Cuál es el mejor enfoque para copiar en profundidad la matriz en Angular 2?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Mira esto:

 let cloned = source.map(x => Object.assign({}, x));
over 4 years ago · Santiago Trujillo Report

0

Sencillo:

 let objCopy = JSON.parse(JSON.stringify(obj));

Esto también funciona (solo para arreglos)

 let objCopy2 = obj.slice()
over 4 years ago · Santiago Trujillo Report

0

Esto es trabajo para mí:

 this.listCopy = Object.assign([], this.list);
over 4 years ago · Santiago Trujillo 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!