如何使用键值对象的键作为 angular 模板中任何其他对象的属性名称

How to use the key of an keyvalue object as propperty name of an other object in angular template

如何使用键值对象的键作为angular模板中另一个对象的属性名

<ion-col *ngFor="let item of items | keyvalue">
{{ (stati | async)?.[item.key] }} // doesn't work
</ion-col>

item.key 是我需要的 属性 的名称 "stati"?

对象示例:

items: { 
  item1: {id: 1, title: "xy", ... }, 
  item1: {id: 2, title: "xy", ... }, 
  item1: {id: 2, title: "xy", ... }
}
stati: { 
  'item1': 'xy1', 
  'item2': 'xy2', 
  'item3': 'xy3' 
}

我需要的: 只有对应key的stati对象的值。

(stati | async) 可以 return undefinednull。 尝试用 if 条件换行:

<ng-container *ngIf="(stati | async) as _stati">
  <ion-col *ngFor="let item of items | keyvalue">
    {{ _stati[item.key] }}
  </ion-col>
</ng-container>

尝试使用 object.keys 和您需要的密钥的索引

let objArr = [
    'apple',
    'microsoft',
    'amazon',
    'alphabet',
    'tencent',
    'alibaba'
];
console.log(Object.keys(objArr))

从键中,您可以从对象中获取相应的值

var requiredKey = Object.keys(objArr)[2] //amazon
var obj = {key1: "value1", key2: "value2"};
Object.assign(obj, {key3: requiredKey});
//There are other ways too

希望这能回答您的问题。