我正在努力弄清楚如何将我的数组键推送到我自己的数组中,我最终会将其用作列标题。
以下内容成功记录了我的密钥,但推送正在破坏
let cols = []
data.forEach(function (obj, index) {
if (index === 0)
{
console.log(Object.keys(obj));
this.cols.push(Object.keys(obj));
}
});
我实际上不必循环整个数据对象,我只需要第一个索引,但一次只需要一步!
我期望的最终结果是cols = [" ValueDate"," AccountName"," Holding"]
请有任何想法
Thnaks GWS
数据提取:
[
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHAUD",
"Holding": 318622.53
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHCAD",
"Holding": 7195
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHEUR",
"Holding": 5077.97
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHGBP",
"Holding": 19625
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHJPY",
"Holding": 16463
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHNZD",
"Holding": 601.56
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHSGD",
"Holding": 1000
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHUSD",
"Holding": 1716906.25
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHAUD",
"Holding": 318622.53
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHCAD",
"Holding": 7195
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHEUR",
"Holding": 5077.97
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHGBP",
"Holding": 19625
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHJPY",
"Holding": 16463
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHNZD",
"Holding": 601.56
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHSGD",
"Holding": 1000
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHUSD",
"Holding": 1720781.25
}
]
答案 0 :(得分:2)
如果您只需要查看第一个索引,只需使用data = [
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHAUD",
"Holding": 318622.53
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHCAD",
"Holding": 7195
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHEUR",
"Holding": 5077.97
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHGBP",
"Holding": 19625
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHJPY",
"Holding": 16463
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHNZD",
"Holding": 601.56
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHSGD",
"Holding": 1000
},
{
"ValueDate": "2017-04-26T14:16:00",
"AccountName": "CASHUSD",
"Holding": 1716906.25
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHAUD",
"Holding": 318622.53
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHCAD",
"Holding": 7195
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHEUR",
"Holding": 5077.97
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHGBP",
"Holding": 19625
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHJPY",
"Holding": 16463
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHNZD",
"Holding": 601.56
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHSGD",
"Holding": 1000
},
{
"ValueDate": "2017-04-27T14:16:00",
"AccountName": "CASHUSD",
"Holding": 1720781.25
}
]
let cols = Object.keys(data[0])
console.log(cols)
即可。见下面的工作示例。
adb shell settings get global multi_sim_voice_call

答案 1 :(得分:0)
使用
for(var key in obj){
}
data.forEach(function (obj, index) {
if(index == 0){
for(var key in obj){
console.log(key);
}
}
});
循环访问对象并获取键值
答案 2 :(得分:0)
让cols = []
data.forEach(function (obj, index) {
if (index === 0)
{
console.log(Object.keys(obj));
cols.push(Object.keys(obj));
}
});
的console.log(COLS)