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);
}
}
精彩评论