jquery - Javascript - Getting object's keys when object is an array of objects -


i having troubles getting key values in block of code similar following:

var somearray = []; somearray.push(objx, objy, objz); //each of these objects pushed in have 1 key/value pair  (var = 0; < somearray.length; i++) {     switch (object.keys(somearray[i][0])) { //not sure "[i][0]" valid?         //now set tags using jquery     } } 

so in above code example passing in array of objects (each object single key/value pair). , want key of each of these can set html tag corresponds each using jquery.

thought: [i] sufficient since array of each object's keys every 1?

any appreciated!!

if each object have 1 enumerable property, can use object.keys(somearray[i])[0] property's name in loop. object.keys returns array of object's own, enumerable property names, , [0] gets first entry it. (and of course, somearray[i][thename] give value of property.)

example:

var objx = {    x: "ecks"  };  var objy = {    y: "why"  };  var objz = {    z: "zee"  };  var somearray = [];  somearray.push(objx, objy, objz);    (var = 0; < somearray.length; i++) {    var arrayentry = somearray[i];    var name = object.keys(arrayentry)[0];    console.log(name + " " + arrayentry[name]);  }


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -