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

639
Views
¿Cómo puedo unir dos objetos en Angular?

¿Cómo unirse al objeto en angular 2?

respuesta de objeto 1

 0:Object 1:Object 2:Object 3:Object

respuesta de objeto 2

 0:Object 1:Object 2:Object 3:Object

componente myComponent

 resultdata :any=Array; fooddrinks_data_func(Defaultparams){ return this.CitiesService.getresult(Defaultparams).subscribe(data => { this.resultdata = this.resultdata.join(data.categorylistvalues); }); }

me sale este error

error_handler.js:59 STACKTRACE ORIGINAL: ErrorHandler.handleError @ error_handler.js:59 next @ application_ref.js:348 schedulerFn @ async.js:93 SafeSubscriber.__tryOrUnsub @ Subscriber.js:234 SafeSubscriber.next @ Subscriber.js:183 Suscriptor ._next @ Subscriber.js:125 Subscriber.next @ Subscriber.js:89 Asunto.siguiente @ Asunto.js:55 EventEmitter.emit @ async.js:79 NgZone.triggerError @ ng_zone.js:333 onHandleError @ ng_zone.js: 294 webpackJsonp.1416.ZoneDelegate.handleError @ zone.js:338 webpackJsonp.1416.Zone.runTask @ zone.js:169 ZoneTask.invoke @ zone.js:420 error_handler.js:60 TypeError: _this.resultdata.join no es Una función

el resultado final debería ser así.

this.resultdata

 0:Object 1:Object 2:Object 3:Object 4:Object 5:Object 6:Object 7:Object
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Creo que estás buscando object.assign() ?

El método Object.assign() se utiliza para copiar los valores de todas las propiedades propias enumerables de uno o más objetos de origen a un objeto de destino. Devolverá el objeto de destino.

 var obj1 = { a: 1 }; var obj2 = { b: 2 }; var merged = Object.assign(obj1, obj2); console.log(merged); // { a: 1, b: 2 }

about 4 years ago · Santiago Trujillo Report

0

Al leer tu código dos detalles me llamaron la atención:

  • los datos de resultados no se escribieron como Array
  • no hubo map() después de la llamada al método getResult, que supongo que es una llamada remota.

Así que esta es mi sugerencia de cambio de código.

 resultdata :Array<any> = []; fooddrinks_data_func(Defaultparams){ return this.CitiesService.getresult(Defaultparams) .map((response) => response.json()) .subscribe(data => { this.resultdata = this.resultdata.join(data.categorylistvalues); }); }

about 4 years ago · Santiago Trujillo Report

0

La respuesta de @Mike funciona bien.

 var obj1 = { a: 1 }; var obj2 = { b: 2 }; var merged = Object.assign(obj1, obj2); console.log(merged); // { a: 1, b: 2 }

pero después de Object.assign obj1 también se vuelve igual que fusionado

 console.log(obj1); // { a: 1, b: 2 }

Puede usar lodash para usar una solución de revestimiento

 var obj1 = {a :1}; var obj2 = { b: 2}; _.merge(obj1, obj2);

El resultado será ---->

 { "a": 1, "b": 2 }

Además de eso, perderá la clave si ambos objetos tienen el mismo nombre de clave.

about 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!