# Index of array in for-each loop

**URL:** <https://forum.kirupa.com/t/index-of-array-in-for-each-loop/280615>\
**Category:** Uncategorized\
**Created:** [January 29, 2009, 1:44pm UTC](https://forum.kirupa.com/t/index-of-array-in-for-each-loop/280615 "2009-01-29T13:44:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nook](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@nook](https://forum.kirupa.com/u/nook)\
**Post date:** [January 29, 2009, 1:44pm UTC](https://forum.kirupa.com/t/index-of-array-in-for-each-loop/280615/1 "2009-01-29T13:44:27Z")

</div>

This is a question spawned from my previous post (you don’t have to read it):

> **[Looking for input about large data handling.](https://www.kirupa.com/forum/showthread.php?p=2432200)**
>
> Got a web design/development question or just want to hang out with the most awesome people on the planet? Drop on in!

Consider an array with x number of elements (of type Object) where some of the elements might be undefined.  
I’d like to use a for-each loop to access all the elements of the array that is not undefined (since it simply skips the undefined elements).  
But. I also want the index of that particular element.  
So I have two options:

1. Use array.indexOf(element).
2. Use traditional for loop, thus getting the index out of the iterator.

I’d rather not use option 2 since it contradicts the whole idea of not having to deal with undefined elements.  
But I’m wondering if option 1 will be quick enough, a feels a bit like looking for water on the other side of the river so to speak.

Can you think of other solutions?

:: nook
