angularjs - How to get index of array element in loop? -


i have loop angular js:

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

how index of array element in loop each item? tried:

angular.foreach($scope.message, function (item, $index) {}); 

sorry vitriol of community. you're close solution bit confused documentation. it's okay, let me clarify!

in documentation angular.foreach see following statement:

invokes iterator function once each item in obj collection, can either object or array. iterator function invoked iterator(value, key, obj), value value of object property or array element, key object property key or array element index , obj obj itself. specifying context function optional.

and 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, code this:

angular.foreach('name of list/array want loop through', 'callback function called each element of list')

the important part you're missing is 'callback...' mentioned above can handed 3 variables can use in callback. callback called each element in list. here explanation of 3 variables:

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

key: - index belonging current item in array

object: the object (or array/list itself)

here example put use key create new string showing index of each letter in $scope.message. hope helped!


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -