Get the first element in an array with an index not starting at 0

I am using a javascript library that returns arrays not starting from scratch, like starting at 26 or 1500, what I want to do is a method to get the first element in this array regardless of the index number starting at 0 or any other number.

Is there any method for this in javascript?

+5
source share
5 answers

The information below is generally useful, but for the problem listed on the OP list, Nina’s answer is by far the best solution.


They are called sparse arrays, and this is one of the few situations where you can use for-in for an array.

Remember that arrays are objects in JavaScript, and array entries are name-based properties (array indexes) that match certain criteria . Therefore, we can use functions that allow us to discover the properties of an object in order to find indexes on your sparse array.

for-in example:

 for (var n in theArray) { if (theArray.hasOwnProperty(n) && isArrayIndex(n)) { // Use theArray[n] } } 

This answer shows how you can determine that n is an array index, and not some other property. A very technical definition would be

 function isArrayIndex(n) { return /^0$|^[1-9]\d*$/.test(n) && n <= 4294967294; } 

... but a definition that is good enough for most of us will be

 function isArrayIndex(n) { return !isNaN(parseInt(n, 10)); } 

Similarly, you can use Object.keys ; since it only looks at its own enumerated properties, you do not need the hasOwnProperty check:

 Object.keys(theArray).forEach(function(n) { if (isArrayIndex(n)) { // ... } }); 

Please note that officially , not one of them is in any particular order, even in ES2015 ("ES6"). Therefore, in theory, you can see indices from a numerical order. In the real world, I have never seen even the vaguely modern JavaScript engine that returned array indexes from a system. They are not obliged, but everyone that I tried does.

So officially, you will need to get a complete list, and then find the minimum value in it:

 var min = Object.keys(theArray).reduce(function(min, n) { var i = parseInt(n, 10); return isNaN(i) || (min !== undefined && min > i) ? min : i; }, undefined); 

This will give you undefined if the array is empty, or the min index if it is not. But if you want to make an assumption, you will get the keys in numerical order:

 // Makes an assumption that may not be true var min = +Object.keys(theArray).filter(isArrayIndex)[0]; 

If you use a JavaScript engine that is fully updated, you can rely on the order returned by Object.getOwnPropertyNames , which required to list array indices in order.

 var min = +Object.getOwnPropertyNames(theArray).filter(isArrayIndex)[0]; 
+2
source

I suggest using Array#some . You get the first opaque element and index. The iteration ends immediately if you return true in the callback:

 var a = [, , 22, 33], value, index; a.some(function (v, i) { value = v; index = i; return true; }); console.log(index, value); 
+4
source

I believe that an alternative to Array.prototype.some() is the Array.prototype.findIndex() method. They are much faster than a single filter, and will not contain your array and indexes.

 var arr = new Array(1000), fi = -1; arr[777] = 1453; // now we have a nice sparse array fi = arr.findIndex(f => f !== void 0); // void 0 is the perfect undefined console.log(fi); console.log(arr[fi]); 
+1
source

It may be useful to use the filter function in the array to return a normalized array.

 var fullArray = array.filter(function(n){ return n != undefined; }); fullArray[0] 

The answers here will help you decide to remove empty elements from an array in javascript

0
source

With this code snippet, you can find the first assigned index of the value and then get the value from your array:

 var a = [, , 22, 33]; var value = a.find((v, i) => i in a); console.log(value); /* ---------------------------------------------- */ var i = 0 while (!(i in a) && i < a.length) i++; // If i === a.length then the array is emtpy console.info(i, a[i]); 

The first implementation uses Array.prototype.find , which makes using the variable variable more clean, but to find the index, you must call indexOf in an array.

But the second is a bit old-fashioned, but it makes it possible to have an index without too much effort.


BTW Nina seems better. (can make it shorter?)

0
source

All Articles