0

I am kind of confused with the foreach loop syntax with the key and value pairs. So I have an object and a foreach loop something like the example below. In my case, what will be my object, value, and key? and what do I need to do if I want to change the name Canada to China?

Array

 $scope.tempData = [];

variables:

var data = [{name: "Kevin", country: "Canada"}, {name:"Bob", country: "Spain"}];


     $scope.editedDetails = function () {

           angular.forEach(object,
     function(value, key) { 

     });

    };

1 Answer 1

1

In your case, your iterating through an array of objects, so the value will be the object at each index, and the key will be the index value (i.e. 0, 1, ...). You want to pass in data for the object you refer to.

var data = [{name: "Kevin", country: "Canada"}, {name:"Bob", country: "Spain"}];

angular.forEach(data, function (value, key) {
  // Key will be 0 or 1
  if (value.country === 'Canada') {
    value.country = 'China';
  }
});

Because objects are passed by reference in JavaScript, you can edit properties on each object directly, i.e. value.country === 'China' will change the object inside the actual array.

However, you can only change properties on the object, and if you tried to overwrite the entire object, it wouldn't work.

var data = [{name: "Kevin", country: "Canada"}, {name:"Bob", country: "Spain"}];

angular.forEach(data, function (value, key) {
  if (value.country === 'Canada') {
    // This won't update the object in data.
    value = {
      name: value.name,
      country: 'China'
    };
  }
});

If you were iterating through an object with non-object types, you'd have to use the key and edit the object directly, and in the case of passing it an object, the key will be the actual key in the object.

var nyc = { name: 'New York City', state: 'NY' };

angular.forEach(nyc, function (value, key) {
  if (key === 'state') {
    nyc[key] = 'New York'; // Or, nyc.state since we know what it is
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hi emptyArsenal thank you for your reply. What if I have some items such as 100 items and each items has its own id. How can I apply to this foreach situation? with the empty array let say var myArray = []; ( findIndexForItem(array, id){ angular.forEach(array, function(index, elem){ if (elem.id = id) return index; } return -1; } )
In that case, you probably don't want to use forEach because it won't return a value. You could set an outside variable with this method, but then you'll have to iterate through the entire array even if you find a match right away. Here's a post on your comment specifically: stackoverflow.com/questions/8668174/…

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.