Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first element of an array?

How do you get the first element from an array like this:

var ary = ['first', 'second', 'third', 'fourth', 'fifth']; 

I tried this:

alert($(ary).first()); 

But it would return [object Object]. So I need to get the first element from the array which should be the element 'first'.

like image 861
MacMac Avatar asked Nov 03 '10 18:11

MacMac


People also ask

What is the first element in an array called?

The memory address of the first element of an array is called first address, foundation address, or base address. Because the mathematical concept of a matrix can be represented as a two-dimensional grid, two-dimensional arrays are also sometimes called matrices.

How do you properly access the first element in an array variable?

You can use find() to get the first element. var firstItem = yourArray.

How do I get an element from an array?

get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned.


2 Answers

like this

alert(ary[0]) 
like image 90
John Hartsock Avatar answered Oct 17 '22 08:10

John Hartsock


Why are you jQuery-ifying a vanilla JavaScript array? Use standard JavaScript!

var ary = ['first', 'second', 'third', 'fourth', 'fifth']; alert(ary[0]); 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Also, needs more jQuery

Source, courtesy of bobince

like image 34
Matt Ball Avatar answered Oct 17 '22 09:10

Matt Ball