0

I have 2 Json object as follows.

var j1={"fname":"XYZ","lname":"qwe"};

and

var j2 = {"id":"10"} 

I want to merge j1 and j2

I tried with var ob = angular.merge({},j1,j2); but its showing angular.merge is not a function

I tried with push and concat. But these also gave same kind of error.

please help, thank you.

2
  • var merged = jQuery.extend(true, j1, j2) Commented Jun 25, 2016 at 11:34
  • how about var ob = angular.extend({}, j1, j2); Also what version of angular are you using? Commented Jun 25, 2016 at 11:39

1 Answer 1

1

If your version of Angular does not support angular.merge yet, you can use angular.extend instead.

angular.module('app', [])
.controller('TestController', ['$scope', function(){
  var j1 = {"fname":"XYZ","lname":"qwe"};
  var j2 = {"id": 10};
  var obj = angular.extend({}, j1, j2);
  
  console.log(obj);
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="TestController">
</div>

Note: angular.merge is only available from version 1.4 to the latest

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you But instead of {} I used an object
yes, you can do that as well. angular.extend(anotherobject, j1, j2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.