Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get property value in js object when key is unknown

I have an array of objects:

a = [
     {81.25: {p:81.25}}, 
     {81.26: {p:81.26}}
    ]

I want to loop through array ang get value of p in each element:

for (var key in a) {
   console.log(a[key]); // outputs {81.25: Object}
   //How do i get p value out of the current element?
}

EDIT: sorry for misleading, I wodnt want to loop againg - thought may be some way to get first object inside current one and to get it's property p.

like image 625
Prosto Trader Avatar asked Mar 13 '14 13:03

Prosto Trader


1 Answers

Use a standard for loop for the array:

for (var i = 0; i < a.length; i++) {
    if (typeof a[i] == object) { //object test
        for (var key in a[i]) {
            if (a[i].hasOwnProperty(key)) {
                console.log(a[i][key]); //here ya go
            }
        }
    }
}
like image 78
tymeJV Avatar answered Oct 19 '22 23:10

tymeJV