Arrays 如何以角度遍历对象的json数组以提取所有对象?
我正在努力显示表中对象数组中的特定对象。我想在两个单独的列中显示“treshold”和“pointsToAdd”的值。当前,我提取的所有内容都显示在一列(而不是两列)中,如下所示: [对象对象],[对象对象],[对象对象],[对象对象对象] 我是一个初学者,不能想出一个最合适的方式来用打字脚本 ======================================================= JSON代码:Arrays 如何以角度遍历对象的json数组以提取所有对象?,arrays,json,angular,loops,object,Arrays,Json,Angular,Loops,Object,我正在努力显示表中对象数组中的特定对象。我想在两个单独的列中显示“treshold”和“pointsToAdd”的值。当前,我提取的所有内容都显示在一列(而不是两列)中,如下所示: [对象对象],[对象对象],[对象对象],[对象对象对象] 我是一个初学者,不能想出一个最合适的方式来用打字脚本 ======================================================= JSON代码: [{ "treshold": [ { "tresho
[{
"treshold": [
{ "treshold": 35, "pointsToAdd": 10 },
{ "treshold": 50, "pointsToAdd": 20 },
{ "treshold": 75, "pointsToAdd": 30 },
{ "treshold": 100, "pointsToAdd": 40 }],
"_id": "5d6f3acd4d94a08064f4bd11",
"is_active": true,
"date": "20190904"
}]
======================================
My component.ts代码:
export class TresholdComponent implements OnInit {
posts: any=[];
treshold: string;
pointsToAdd: string;
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('http://urladdress').subscribe(posts =>{
this.posts = posts;})
}
}
=======================================
My component.html代码:
<table class="table table-borderless user-table">
<thead class="thead-border">
<tr>
<div class="d-flex justify-content-center table-checkbox">
<mat-checkbox class=""></mat-checkbox>
</div>
<td scope="col">Treshold</td>
<td scope="col">Points to add</td>
</tr>
</thead>
<tbody>
<tr *ngFor="let post of posts">
<div class="d-flex justify-content-center table-checkbox">
<mat-checkbox></mat-checkbox>
</div>
<td>{{post.treshold}}</td>
<td>{{post.pointsToAdd}}</td>
</tr>
</tbody>
</table>
特雷肖尔德
要补充的要点
{{post.treshold}}
{{post.pointsToAdd}
看起来您正在迭代第1个数组,treshold和pointsToAdd位于表内的内部数组(属于treshold)DIV中,而超出TDs看起来像是一个错误(可能是无效的HTML),如果您在init中设置该数组会怎么样?this.posts=posts。treshold@BillCheng这就是我发现的问题。如何遍历该数组的内部数组?汤姆:我没有复制附件,而是删除了原始的html来发布在这里。使用“this.posts=posts.treshold”会导致完全不显示任何内容以使其工作,您可以尝试this.posts=posts[0]。treshold
,但存在一个问题。。。您需要确保POST是一个数组,并且长度至少为1。如果posts数组中有多个元素呢?您也必须渲染它们吗?抱歉,没有注意到,或者您可以在case中伪造它,例如(!this.posts | | |!this.posts.length>0)this.posts=[{“treshold”:“,”pointsToAdd:“}];看起来您正在迭代第1个数组,treshold和pointsToAdd在表内的内部数组(treshold的)DIV中,而在TDs之外看起来像是一个错误(可能是无效的HTML),如果您在init中设置该数组呢?this.posts=posts。treshold@BillCheng这就是我发现的问题。如何遍历该数组的内部数组?汤姆:我没有复制附件,而是删除了原始的html来发布在这里。使用“this.posts=posts.treshold”会导致完全不显示任何内容以使其工作,您可以尝试this.posts=posts[0]。treshold
,但存在一个问题。。。您需要确保POST是一个数组,并且长度至少为1。如果posts数组中有多个元素呢?您也必须渲染它们吗?抱歉,没有注意到,或者您可以在case中伪造它,例如(!this.posts | | |!this.posts.length>0)this.posts=[{“treshold”:“,”pointsToAdd:“}];