开发者

Accessing key in an Array using JavaScript

RaceArray: [{
    Unknown: 0,
    C开发者_JAVA技巧aucasian: 1,
    AfricanAmerican: 2,
    AmericanIndian: 3,
    Asian: 4,
    Hispanic: 5,
    Other: 6
 }]

How can i access the key's alone in my JavaScript and form it as an separate array.

Desired outcome...

RaceArray = ['Unknown','Caucasian']


var RaceObj = [{
    Unknown: 0,
    Caucasian: 1,
    AfricanAmerican: 2,
    AmericanIndian: 3,
    Asian: 4,
    Hispanic: 5,
    Other: 6
}];

var obj = RaceObj[0], Keys = [];
for(var key in obj){
  if(obj.hasOwnProperty(key)){
    Keys.push(key);
  }
}


There may be a simpler solution but you can do:

var keys = [];
for(var key in RaceObj) {
    if (RaceObj.hasOwnProperty(key)) {
        keys.push(key);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜