问题描述
我不想像这样使用 for 循环将对象转换为数组!如果将过程加倍并降低应用程序的性能(我正在使用 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(让输入数据){数组推(值);}
for(let key in data) { array.push(value);}
是否有任何解决方案可以使用 *ngFor 迭代对象本身(如附图所示).
Is there any solution to iterate object itself(shown in picture attached) using *ngFor.
或者我可以将此对象(如附图所示)转换为数组,以便在 *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 Convert Object to Array - 因为 *ngFor 不支持对象的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!