How can I find out the position of an item contained by an array?


I know the name of the item and would like to know its position in the array, how would I do that?


["a","b","c","d","e"].indexOf("c") 2


You could loop through the array:

var array = ['item1', 'item2', 'item3']; function findIndex(array, item) { for (var i = 0; i < array.length; i++) { if (array[i] === item) { return i; } } return -1; } alert(findIndex(array, 'item2'));

Or using a comparer function for more complex types:

var array = ['item1', 'item2', 'item3']; function findIndex(array, comparer) { for (var i = 0; i < array.length; i++) { if (comparer(array[i])) { return i; } } return -1; } var index = findIndex(array, function(item) { return item === 'item2' ; });


Use the indexOf method of Array. If it's not there add it (code example <a href="http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/" rel="nofollow">here</a> or other places).

If you want to find a value by its name property, do this:

for (var i = 0; i < this.length; i++) { if(this[i].name == name) { return i; } } return -1;


  • Redux - Removing object from array nested in another array
  • F# take items from a sequence
  • How to authenticate a route with passport-jwt?
  • How do I add each price to the current price?
  • Pandas DataFrame to List of Dictionaries
  • Get option from specific start position?
  • Entity framework, referential integrity constraint violation occurred error on updating entity from
  • Specific pathing to find XML elements using minidom in Python
  • Remove property by value from object
  • Pylint warnings on inherited nested class members
  • How to add custom images as UIApplicationShortcutIcon for UIApplicationShortcutItem?
  • How to invoke ActionListener for MenuItem in primefaces
  • TypeScript 2.0 throws errors from excluded files?
  • Changing the color of an image link on hover
  • Get sibling of Vaadin Tree Item?
  • inserting items in list in mongodb document
  • Blackberry - Custom EditField Cursor
  • Body moving without any force applied? (Box2d)
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • retrieve vertices with no linked edge in arangodb
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?