Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first Element of an array - jQuery

I have a multidimensional and I want to get the first element of the 2nd dimension.

myArray[0]

is my 2nd dimension. I am not able to use

myArray[0][0]

because I don't know the key of the 2nd dimenson (key = userID). Any idea how to get the first element without knowing the key?

like image 838
Chris Avatar asked Dec 07 '25 07:12

Chris


1 Answers

Object properties (JavaScript does not have true multidimensional or associate arrays) have no defined order, and can only be accessed via their string key.

The order of iterating over keys with a for ( in ) is implementation specific as no order is specified in the specification. For example, Chrome orders numeric keys no matter what order they are added (as an array related optimisation for V8).

If you decide you don't want to listen to me and want to live dangerously, you could get the first property according to the JavaScript implementation with var worksExceptWhenItDoesnt = myArray[Object.keys(myArray)[0]].

like image 150
alex Avatar answered Dec 08 '25 23:12

alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!