如何从字段数组中获取字段名称

时间:2016-07-22 19:51:22

标签: javascript jquery arrays

像我这样的对象

var obja = [{USER:"bob",  SCORE:2000, TIME:32,    AGE:16, COUNTRY:"US"},`{USER:"jane", SCORE:4000, TIME:35,    AGE:16, COUNTRY:"DE"},`{USER:"tim",  SCORE:1000, TIME:30,    AGE:17, COUNTRY:"UK"},` {USER:"mary", SCORE:1500, TIME:31,    AGE:19, COUNTRY:"PL"}];

但我想只存储USER,SCORE,TIME,AGE,COUNTRY字段到数组

1 个答案:

答案 0 :(得分:0)

您可以通过这种方式获取对象密钥

var obja = [{USER:"bob",  SCORE:2000, TIME:32,    AGE:16, COUNTRY:"US"}, {USER:"jane", SCORE:4000, TIME:35,    AGE:16, COUNTRY:"DE"},{USER:"tim",  SCORE:1000, TIME:30,    AGE:17, COUNTRY:"UK"}, {USER:"mary", SCORE:1500, TIME:31,    AGE:19, COUNTRY:"PL"}];

for(var key  in obja[0]){ 
 console.log(key)
}