Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I join two objects in Angular?

how to join to object in angular 2?

object response 1

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

object response 2

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

myComponent component

resultdata :any=Array;

 fooddrinks_data_func(Defaultparams){

    return this.CitiesService.getresult(Defaultparams).subscribe(data => {                
         this.resultdata = this.resultdata.join(data.categorylistvalues);

    });  
}

I get this error.

error_handler.js:59 ORIGINAL STACKTRACE: 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 Subscriber._next @ Subscriber.js:125 Subscriber.next @ Subscriber.js:89 Subject.next @ Subject.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 is not a function

the Final result should be like this.

this.resultdata

0:Object
1:Object
2:Object
3:Object
4:Object
5:Object
6:Object
7:Object
like image 390
Vel Avatar asked Apr 28 '17 11:04

Vel


People also ask

How do you join two objects together?

To merge objects into a new one that has all properties of the merged objects, you have two options: Use a spread operator ( ... ) Use the Object. assign() method.

How do I merge two objects in TypeScript?

Use the spread syntax (...) to merge objects in TypeScript, e.g. const obj3 = { ... obj1, ... obj2 } . The type of the final object will successfully be inferred, so trying to add or remove properties from it will cause the type checker to show an error.

What does angular merge do?

angular. merge is an Angular 1.4+ API that is to deep (recursively) copy the properties of the source objects to the destination object.

How do you combine objects in JavaScript?

The easiest way to merge two objects in JavaScript is with the ES6 spread syntax / operator ( ... ). All you have to do is insert one object into another object along with the spread syntax and any object you use the spread syntax on will be merged into the parent object.


1 Answers

I think you are looking for object.assign()?

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

var obj1 = { a: 1 };
var obj2 = { b: 2 };

var merged = Object.assign(obj1, obj2);

console.log(merged); // { a: 1, b: 2 }
like image 142
Mike Bovenlander Avatar answered Sep 24 '22 13:09

Mike Bovenlander