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
Javascript/Jquery Check Broken Links
Angularjs $Resource Restful Example
How to Create a Custom Error in JavaScript
Clearing Localstorage in JavaScript
How to Make a Div Element Editable (Like a Textarea When I Click It)
How to Set Image Source with Base64
Editing in the Chrome Debugger
Call a Vue.Js Component Method from Outside the Component
Convert MySQL Datetime Stamp into JavaScript's Date Format
React Input Defaultvalue Doesn't Update with State
Why Do Arrow Functions Not Have the Arguments Array
Es6 Class Multiple Inheritance
JavaScript Replace with Reference to Matched Group
Logging Clientside JavaScript Errors on Server