【发布时间】:2018-11-13 17:47:20
【问题描述】:
下面是我的json
[{
"_hits": 2.163,
"_type": "data",
"_id": "11138",
"_source": {
"urls": "http://localhost:9618/info?data_id=11138",
"host": "max",
"roll": "11138",
"information": {
"type": "image/jpeg",
"data_id": "11138",
"data_size": 186497,
"creation_utctime": "1494831805258",
},
"subhost": "sample"
},
"_index": "max"
}
];
通过使用上述内容,我想将其存储在一个变量中,并希望将其用于其他目的。所以在按钮上点击我正在处理数据
<button type="button"(click)="getData()" >get Data</button>
getData(){
this.rows = [];
for (var res in this.info){
var row = {};
for (var key in this.info[res]['_source']){
for (var k in this.info[res]['_source'][key]){
let temp = key + "." + k;
row[temp] = this.info[res]['_source'][key][k];
}
row['_id'] = this.info[res]['_id'];
}
this.rows.push(row);
console.log(this.rows);
}
}
要求的输出是:
host:"max"
information.creation_utctime: "1494831805258"
information.data_id: "11138"
information.data_size: 186497
information.type: "image/jpeg"
roll:"11138"
subhost:"sample"
urls:"http://localhost:9618/info?data_id=11138"
_id: "11138"
我得到的输出是:
host.0: "m"
host.1: "a"
host.2: "x"
information.creation_utctime: "1494831805258"
information.data_id: "11138"
information.data_size: 186497
information.type: "image/jpeg"
roll.0: "1"
roll.1: "1"
roll.2: "1"
roll.3: "3"
roll.4: "8"
subhost.0: "s"
subhost.1: "a"
subhost.2: "m"
subhost.3: "p"
subhost.4: "l"
subhost.5: "e"
urls.0: "h"
urls.1: "t"
urls.10: "a"
urls.11: "l"
urls.12: "h"
urls.13: "o"
urls.14: "s"
urls.15: "t"
urls.16: ":"
urls.17: "9"
urls.18: "6"
urls.19: "1"
urls.2: "t"
urls.20: "8"
urls.21: "/"
urls.22: "i"
urls.23: "n"
urls.24: "f"
urls.25: "o"
urls.26: "?"
urls.27: "d"
urls.28: "a"
urls.29: "t"
urls.3: "p"
urls.30: "a"
urls.31: "_"
urls.32: "i"
urls.33: "d"
urls.34: "="
urls.35: "1"
urls.36: "1"
urls.37: "1"
urls.38: "3"
urls.39: "8"
urls.4: ":"
urls.5: "/"
urls.6: "/"
urls.7: "l"
urls.8: "o"
urls.9: "c"
_id: "11138"
下面是我的stackblitzurl
https://stackblitz.com/edit/angular-d7mnpz
所以这里我想要上面的数据需要输出,我得到上面的输出
【问题讨论】:
-
你不需要第二个 for 循环。
-
@lbu 好的,但我希望输出是为此目的所需的输出,重要的是要正确
标签: javascript json angular