16

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) {});
8
  • 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 Commented Apr 25, 2015 at 16:58
  • 2
    You tried that, and what happened? Commented Apr 25, 2015 at 16:59
  • And if it is not working, provide enough code to replicate the problem Commented Apr 25, 2015 at 17:02
  • 1
    How do you think, if I have written code in question it means that i read doc. But $index does not work for me Commented Apr 25, 2015 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... Commented Apr 25, 2015 at 17:04

4 Answers 4

50

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!

Sign up to request clarification or add additional context in comments.

2 Comments

why are you apologizing for others when the question itself is incomplete and doesn't present an mvce
Many people look at StackOverflow before the official documentation , sometimes there is not even documentation although it is not the case
12
angular.forEach($scope.arrayname,function(item,index){
  console.log(item,index)
})

4 Comments

Hello and welcome to StackOverflow. Please provide a explanation to your answer to be more valualbe for other users. See stackoverflow.com/help/how-to-answer
ya sure index is the default property angular.forEach() you can also get use i instead of of index also u can get index of object in array
Please note that comments are temporary and could be deleted anytime. If you have additional information to provide, please update your answer by clicking on the "edit" link under the post. See here for details. Thank you.
This answer only applies object you are iterating over is an array. If this were iterating over an Object object then the value of index would be the key for that iteration.
0

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;
}); 

Comments

0
var items = ['a','b','c','d','e','f']
angular.forEach(items,function(item,index){
      console.log(item)
      console.log(index)
     }

Comments

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.