Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an loop Angular JS:

angular.forEach($scope.message, function (item) {
      return (item.id_user == input.id_user) ? true : false;
}); 

How to get index of array element in loop for each item? I tried:

angular.forEach($scope.message, function (item, $index) {});
share|improve this question
1  
DId you even look in the docs? It is provided as argument of forEach. Please put a little more research effort in before asking questions here. When a simple issue like this can be looked up in the manual...then the question should not even be here docs.angularjs.org/api/ng/function/angular.forEach – charlietfl Apr 25 '15 at 16:58
2  
You tried that, and what happened? – JB Nizet Apr 25 '15 at 16:59
    
And if it is not working, provide enough code to replicate the problem – charlietfl Apr 25 '15 at 17:02
    
How do you think, if I have written code in question it means that i read doc. But $index does not work for me – Popov Aliev Apr 25 '15 at 17:04
    
It works fine, as documented: plnkr.co/edit/79PI8C6WeVaNHQ7Kp3Pn?p=preview. Something is wrong with your code, but you didn't provide any way to find out what, so... – JB Nizet Apr 25 '15 at 17:04
up vote 19 down vote accepted

Sorry for all the vitriol of the community. You're very close to your solution but are a bit confused by documentation. It's okay, let me help clarify!

In the documentation for angular.forEach you will see the following statement:

Invokes the iterator function once for each item in obj collection, which can be either an object or an array. The iterator function is invoked with iterator(value, key, obj), where value is the value of an object property or an array element, key is the object property key or array element index and obj is the obj itself. Specifying a context for the function is optional.

And then the following example:

var values = {name: 'misko', gender: 'male'};
var log = [];
angular.forEach(values, function(value, key) {
  this.push(key + ': ' + value);
}, log);
expect(log).toEqual(['name: misko', 'gender: male']);

Essentially, the code is like this:

angular.forEach('name of list/array you want to loop through', 'callback function to be called for each element of the list')

The important part that you're missing is that the 'callback...' mentioned above can be handed 3 variables which you can then use in your callback. Your callback will be called for each element in the list. Here is some explanation of those 3 variables:

Value: The value of the i-th element/property in the list/array/object

Key: i - the index belonging to the current item in the array

Object: the the object itself (or array/list itself)

Here is an example i put together for you where I use the Key to create a new string showing the index of each letter in $scope.message. Hope this helped!

share|improve this answer
    
why are you apologizing for others when the question itself is incomplete and doesn't present an mvce – charlietfl Apr 25 '15 at 18:55
    
Many people look at StackOverflow before the official documentation , sometimes there is not even documentation although it is not the case – Andy.Diaz Nov 27 '15 at 14:50

There is a way.

var index = 0;
angular.forEach($scope.message, function (item) {
  return (item.id_user == input.id_user) ? index : false;
  index = index + 1;
}); 

Here it will return $scope.message index value if item.id_user == input.id_user else returns false. You can also assign $scope.message[index] to some other scope variable like this

var index = 0;
angular.forEach($scope.message, function (item) {
  if(item.id_user == input.id_user){
     $scope.message[index] = $scope.yourVariable;
  }  
  index = index + 1;
}); 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.