Question

I have the following:

$scope.book1 = {
  a = 1,
  b = 2
}

I'm getting this from my database:

$scope.book2 = {
  title = 2,
  author = 'joe'
}

How can I update the book1 object with the data from book2 so that it has all four fields?

Note that my users are using IE9 and above. If it's possible I would like to use lodash but I'm not sure that's possible as I want the result to go into the book1 object and not to create a new object.

Was it helpful?

Solution

If you need to create your extend function

Object.prototype.extend = function(x) {
    for(i in x)
        this[i] = x[i];
};

$scope.book1.extend($scope.book2);

If you are using angularjs you can have a look at http://docs.angularjs.org/api/angular.extend

OTHER TIPS

angular.extend($scope.book1, $scope.book2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top