[英]how to get specific json data from http response
在我的應用程序中,我從 http 請求中獲取數據。 在我的 http 響應中,我有 500 個值的 json 數組。 數組的每個索引都有 10 個值。 我正在嘗試獲取數組中每個索引的特定值並將其推送到另一個數組中。 最好的方法是什么。
constructor(private test: TestService){
this.test.getdata().subscribe(data => {console.log(data);})
}
data = [{t1: 20, t2: 30, tt4: 50, t6:60...t10:100},
{t1: 600, t2: 30, tt4: 50, t6:60...t10:100},
{t1: 200, t2: 30, tt4: 50, t6:60...t10:100}
]
// my new array should look like this. it should contain only the values of t1 for each index.
arr = [20, 600, 200]
您可能需要map
:
var data = [{t1: 20, t2: 30, tt4: 50, t6:60}, {t1: 600, t2: 30, tt4: 50, t6:60}, {t1: 200, t2: 30, tt4: 50, t6:60}]; var result = data.map(({t1})=>t1); console.log(result);
嘗試這個..
var arr = [];
this.data.map((item) => {
this.arr.push(item.t1);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.