Typescript将对象转换为数组 - 因为* ngFor不支持对象的迭代 [英] Typescript Convert Object to Array - because *ngFor does not supports iteration of object

查看:2514
本文介绍了Typescript将对象转换为数组 - 因为* ngFor不支持对象的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我不想使用for循环将Object转换为数组!如果加倍过程并降低app的性能(我使用Ionic2和Typescript,使用Firebase)

  • I don't want to use for loop to convert Object to Array like this! If doubled the process and slow down the performance of app (I'm using Ionic2 and Typescript, with Firebase)

for(let key in data){
的Array.push(值);
}

for(let key in data) { array.push(value); }

是否有任何解决方案可以使用* ngFor迭代对象本身(如附图所示)。

Is there any solution to iterate object itself(shown in picture attached) using *ngFor.

或者我可以将这个Object(如附图所示)转换为Array,这样就可以在* ngFor中进行迭代。

Or I can convert this Object(shown in picture attached) to Array, so that can be iterable in *ngFor.

推荐答案

您可以使用 Object.keys (obj)获取命名索引。这将返回一个数组结构,您可以进一步使用/自定义。迭代对象值的示例用法可能如下所示

You can use Object.keys(obj) to get named indexes. This will return an array structure which you can use/customize further. A sample use to iterate over object values may look like this

var persons = { 
    john: { age: 23, year:2010},
    jack: { age: 22, year:2011},
    jenny: { age: 21, year:2012}
}

获取迭代器

var resultArray = Object.keys(persons).map(function(personNamedIndex){
    let person = persons[personNamedIndex];
    // do something with person
    return person;
});

// you have resultArray having iterated objects 

这篇关于Typescript将对象转换为数组 - 因为* ngFor不支持对象的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆