How to Get the First Element of an Array

Get the first element of an array

Original answer, but costly (O(n)):

array_shift(array_values($array));

In O(1):

array_pop(array_reverse($array));

Other use cases, etc...

If modifying (in the sense of resetting array pointers) of $array is not a problem, you might use:

reset($array);

This should be theoretically more efficient, if a array "copy" is needed:

array_shift(array_slice($array, 0, 1));

With PHP 5.4+ (but might cause an index error if empty):

array_values($array)[0];

How to get the first element of an array?

like this

alert(ary[0])

Map the first element of each array of an array of arrays in Javascript

You could return just the first elementn of x, an element of the outer array.

var myData = [["name1", 34.1, 43.1, 55.2], ["name2", 5.3, 23.6, 40.9], ["name3", 43.5, 77.8, 22.4]],    arrayTitle = myData.map(function(x) {        return x[0];    });
console.log(arrayTitle);

How to get the first element of a array?

I'm not familiar with pm2, but if the array is JSON-compatible (which it looks like it is), you can use jq, for example:

$ echo '[2]' | jq '.[0]'
2
$ echo '[3, 2]' | jq '.[0]'
3

Here's a related question with some other methods: get the first (or n'th) element in a jq json parsing

Get the index of the first element in an array with value greater than x

You can use findIndex here

check this snippet

var array = [400, 4000, 400, 400, 4000];var index=array.findIndex(function(number) {  return number > 400;});console.log(index);


Related Topics



Leave a reply



Submit