Alex
Alex

Reputation: 105

angularjs add item to object array

I have a set of array and object that look like this

`

var PaymentContent = "Payments":
     [{
        "Details": {
                    "PaymentType": "CreditCard",
                    "Amount": $scope.total,
                    "CCNAME": $scope.Form.CreditCard.FullName,
                      }
      }]
Payments: Array[1]
0:Object
 Details: Object
 Amount: 5.99
 CCNAME: null
 PaymentType: "CreditCard"`

Now, how can i update that set of objects and array using angularjs? desired output :

Payments: Array[1] 0:Object Details: Object Amount: 5.99 CCNAME: null PaymentType: "CreditCard" LastPayment: "04/11/2011"

Notice the lastpayment field.

Here is my code

var paymentDetails = {LastPayment : '04/11/2011', LastSignOn : '04/11/2011'} fields = angular.extend({}, PaymentContent , paymentDetails);

Thanks!

Upvotes: 0

Views: 1251

Answers (2)

charlietfl
charlietfl

Reputation: 171679

You are setting an empty object as destination. This new object will receive the properties and values of the other 2 objects...without changing those other 2 source objects

If you want the array object to receive the updates remove the first argument ( the empty object)

angular.extend( fields.Payments[0].Details, paymentDetails);

This will update fields.Payments[0].Details with all of the properties and values in paymentDetails

Demo Fiddle

Upvotes: 2

HaRdik Kaji
HaRdik Kaji

Reputation: 425

You can directly write below code:

Payments[0].LastPayment = "04/11/2011";

Upvotes: 2

Related Questions