1

How can I add properties to objects in an array by looping through the array?

Example I have an array with objects; $scope.addresses contains many addresses.

And lets say that each object has only have 4 properties;

$scope.addresses[0].address_line1
$scope.addresses[0].address_line2
$scope.addresses[0].address_line3
$scope.addresses[0].city

Now I want to add more properties to each of the objects in the array.

$scope.addresses[0].location
$scope.addresses[0].neighbourhood

Any help appreciated.

asked Jun 7, 2016 at 12:43
3
  • Perfect use case for Array.prototype.map() Commented Jun 7, 2016 at 12:54
  • What is source of the property values? Commented Jun 7, 2016 at 12:58
  • I just want to manually add a property and set it to true or false Commented Jun 7, 2016 at 12:59

4 Answers 4

3

Just assign a new property

$scope.addresses[0].location = 'Roxbury'

$scope.addresses[0].location must already be an object since you are setting properties on it.

To loop over addresses, use forEach or a standard for loop:

$scope.addresses.forEach(function(address, index) {
 address.location = locations[index];
});
for(var i=0; i < $scope.addresses.length; i++) {
 $scope.addresses[i].location = myLocations[i];
}

Or you could replace the entire addresses object using map

scope.addresses = addresses.map(address => Object.assign(
 {location: 'X'}, address));
answered Jun 7, 2016 at 12:44
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I can assign a property like you mentioned. But that will be for just one of the object. Imagine I have 100 addresses in $scope.addresses how will I do it? I can't do it manually by adding [0], [1] etc... So I would need to loop and add to all
@shady87 That's correct, you don't know how to loop over the addresses?
Not in angular I'm a PHP developer
1

Use .map to loop the collection:

$scope.addresses = $scope.addresses.map(function(address) {
 address.location = "X"; 
 return address;
});
answered Jun 7, 2016 at 12:48

3 Comments

You're modifying the addresses, may as well use a forEach and drop the return...
Or more simply addresses.map(address => Object.assign({location: 'X'}, address))
After your change it still looks silly, you're returning a new array that contains the same references because you are modifying items in place so both arrays are affected. Still should be using a forEach, or you should be returning modified objects in the map function. Sorry, but I really dislike the misuse of Array#map "use map to loop over a collection" is really bad advice, "use map to create a new array with modified items" is much more true to map's semantics
0

Simply using the . dot notion should do.

$scope.addresses[0].location = 'Iceland';

Just check for existence of $scope.addresses[0] before you do the assignment though otherwise you will get trying to access property of 'undefined' errors.

so something like

if($scope.addresses[0])
 $scope.addresses[0].location = 'Iceland';
answered Jun 7, 2016 at 12:48

Comments

0

You can use forEach loop and then add required items inside it:

angular.forEach($scope.addresses, function(v, k) {
 v.location = "New location " + (k + 1);
 v.neighbourhood = "New neighbourhood " + (k + 1);
 });

Watch demo here.

answered Jun 7, 2016 at 13:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.