简体   繁体   English

如何通过使用angular2 +中的键获取数组内对象的值

[英]How to get the value of object inside an array by using its key in angular2+

var arr=[
        {Key0: "myValue1"},
        {Key0: "myValue2"},
        {Key0: "myValue3"},
        {Key1: "myValue4"},
        {Key2: "myValue5"}, ]

from the above array, I want to get the values using its key in *ngFor angular, for example, key0 values I want to display along with other values values, 从上面的数组中,我想使用* ng中的键获取值。例如,对于角度,我想与其他值一起显示的key0值,

I have also tried like this but can't able to get the desired output 我也这样尝试过,但无法获得所需的输出

                         <li mat-list-item *ngFor='let C of arr'>
                             {{C}}</li>

Note: 注意:

I tried like {{c.key0}} but it prints only key0 values, but here I want all the values like key1,key2 我尝试过像{{c.key0}}但是它只打印key0值,但是在这里我想要所有的值,例如key1,key2

thanks in advance. 提前致谢。

As shown in: How to iterate object keys using *ngFor 如所示: 如何使用* ngFor迭代对象键

You can use (angular v6+): 您可以使用(角度v6 +):

<div *ngFor='let C of arr | keyvalue">
   {{C.key}}:{{C.value}}
</div>

I think you should modify your array in typescript first by using iteration logic. 我认为您应该首先使用迭代逻辑来修改打字稿中的数组。 And then show it using *ngFor. 然后使用* ngFor显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM