javascript - Get property with largest index key in object -


this question has answer here:

from following object, how can 'air' reading property largest index key i.e. 4? (30.9)

{   1: {     air: "31.2",     evap: "25.3",     t_condenser: "43.8"   },   2: {     air: "31.1",     evap: "25.3",     t_condenser: "43.6"   },   3: {     air: "31.0",     evap: "25.3",     t_condenser: "43.5"   },   4: {     air: "30.9",     evap: "25.2",     t_condenser: "43.3"   } } 

i have tried following, try last key, reads undefined:

$.getjson("handler.php?action=fetchlae", function(data){    console.log(data.length);  }); 

note: data badly formatted. thats array for, api returns object.

solution: find highest key in data ( wich object):

data[object.keys(data).sort().pop()].air 

explanation:

object.keys(data) 

get properties array e.g. [1,4,3,2].

now sort ascending , take last:

.sort().pop() 

so take property out of object, , take air value

data[4].air 

http://jsbin.com/setibevozu/edit?console


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -