Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  130] [ 3]  / answers: 1 / hits: 41535  / 11 Years ago, thu, november 21, 2013, 12:00:00

I have a JavaScript object which has a list of retailers



var listRetailers = [
{url:http://www.fake1.com, img:images/1logo.jpg},
{url:http://www.fake2.com, img:images/2logo.gif},
{url:http://www.fake3.com, img:images/3logo.gif},
]


I would like to PUSH a new key:value into each item:



object.push(storeNumber: 1);


So the updated JavaScript object will be



var listRetailers = [
{url:http://www.fake1.com, img:images/1logo.jpg, storeNumber:1},
{url:http://www.fake2.com, img:images/2logo.gif, storeNumber:1},
{url:http://www.fake3.com, img:images/3logo.gif, storeNumber:1},
]


Within my angular controller I have



$scope.retailers = listRetailers ;

angular.forEach($scope.retailers, function(obj){
obj.push(storeNumber: 1);
});


The error states: Object # has no method 'push'



What am I missing here?


More From » angularjs

 Answers
62

That's because obj refers to your retailer object and is not an array. If you want to add properties to it, you can just define them using either the bracket [] notation, or by using the dot . notation.



angular.forEach($scope.retailers, function(obj){

//Using bracket notation
obj[storeNumber] = 1;

//Using dot notation
obj.storeNumber = 1;

});

[#74152] Tuesday, November 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kirstendevanb

Total Points: 585
Total Questions: 93
Total Answers: 94

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
;