Angular 通过循环获取对象的键和对象的值?
我在一个数组中循环,通过循环我得到了键,并且用这个键我想从数组中得到对象的值Angular 通过循环获取对象的键和对象的值?,angular,ionic3,Angular,Ionic3,我在一个数组中循环,通过循环我得到了键,并且用这个键我想从数组中得到对象的值 <ion-list-header *ngFor="let child of item.value[item.key]; let j = index" no-padding> <h2> {{ child['layoutName'] }} </h2> <h1>{{ res
<ion-list-header *ngFor="let child of item.value[item.key]; let j = index" no-padding>
<h2>
{{ child['layoutName'] }}
</h2>
<h1>{{ result[ child['layoutName']] }}</h1>
</ion-list-header>
child['layoutName']
具有密钥ex.light1
,light2
按原样,您可以创建一个过滤器管道,该管道将为给定密钥的数组返回正确的元素。过滤器管道通常是由于性能原因造成的,但如果阵列很小,则无所谓
还有另一种选择:您可以将原始数组转换为对象,以便直接使用它
this.result = { "light1" : true,"light2" : true,"light3" : true}
这样就行了
<h1>{{ result[ child['layoutName']] }}</h1>
{{result[child['layoutName']]}
使用Object.key
获取对象key
.ts文件
Object = Object;
result = [{"light1" : true},{"light2" : true},{"light3" : true}];
<ion-list-header *ngFor="let child of result; let j = index" no-padding>
<ng-container *ngFor="let keys of Object.keys(child)">
<p>Keys - {{keys}}</p>
<p>value - {{child[keys]}}</p>
</ng-container>
</ion-list-header>
.html文件
Object = Object;
result = [{"light1" : true},{"light2" : true},{"light3" : true}];
<ion-list-header *ngFor="let child of result; let j = index" no-padding>
<ng-container *ngFor="let keys of Object.keys(child)">
<p>Keys - {{keys}}</p>
<p>value - {{child[keys]}}</p>
</ng-container>
</ion-list-header>
键-{{Keys}
值-{{child[keys]}
item中的数据是什么?例如,child['layoutName']有light1,我想打印'true',这很好,但当您在item.value[item.key]上迭代时。。所以我的问题是项目中有什么…@nitish哪种方法?将其转换为对象?你有什么错误吗?