views:

91

answers:

3

I have an array of objects in javascript. I use jquery.

How do i get the first element in the array? I cant use the array index - as I assign each elements index when I am adding the objects to the array. So the indexes arent 0, 1, 2 etc.

Just need to get the first element of the array?

+3  A: 

If you don't use sequentially numbered elements, you'll have to loop through until you hit the first one:

var firstIndex = 0;
while (firstIndex < myarray.length && myarray[firstIndex] === undefined) {
    firstIndex++;
}
if (firstIndex < myarray.length) {
    var firstElement = myarray[firstIndex];
} else {
    // no elements.
}

or some equivalently silly construction. This gets you the first item's index, which you might or might not care about it.

If this is something you need to do often, you should keep a lookaside reference to the current first valid index, so this becomes an O(1) operation instead of O(n) every time. If you're frequently needing to iterate through a truly sparse array, consider another data structure, like keeping an object alongside it that back-maps ordinal results to indexes, or something that fits your data.

quixoto
Watch out for empty arrays.
Douglas
Thanks, edited.
quixoto
This may be horribly inefficient depending on the lowest index.
Jasper
@Jasper: Of course it may be; as noted, it's O(n) in the length of the array. But it's the best you're going to do given the data structure.
quixoto
@quixito: that would be O(n) in the first # array element, which can be higher than the number of elements in the array, but is lower than array.length as long as there is more than one element. However, since the specification do not require a certain array efficiency, it does not need to be such. Additionally, the fact that it is a sparse array actually makes that it is probably slower than that.
Jasper
+2  A: 

Shift();

See http://www.bennadel.com/blog/1796-Javascript-Array-Methods-Unshift-Shift-Push-And-Pop-.htm

quixoto is right, this won't work in your case due to shift returning undefined.

How about something simple like this:

var result = myArray.filter(function(e, i, a) { return e != undefined;})[0];

That should return the first non-undefined entry in the array regardless of index.

Jeff Sheldon
-1. Incorrect; this does not answer the question. OP wants the first "valid" item, whatever index it is at (not necessarily zero). `shift` will only pull off whatever is at element 0, including `undefined`.
quixoto
My mistake, edited for an alternative, rather than adding another answer that might mislead others.
Jeff Sheldon
A: 

If you find yourself needing to do manipulation of arrays a lot, you might be interested in the Underscore library. It provides utility methods for manipulating arrays, for example compact:

var yourArray = [];
yourArray[10] = "foo";
var firstValue = _.compact(yourArray)[0];

However, it does sound like you are doing something strange when you are constructing your array. Perhaps Array.push would help you out?

Douglas