![](/img/trans.png)
[英]How do I cast a list of JSON objects into a list of TypeScript objects without losing properties on the TypeScript class?
[英]How to take a list of Objects and create a JSON of specific properties of the objects in the list?
我有一個看起來像這樣的列表:
[ Person({ getName: Function, id: '310394', age: 30 }), Person({ getName: Function, id: '244454', age: 31 })...]
現在我要這樣:
{
peopleIds: [
244454,244454...
]
}
我做了這樣的事情:
public makePeopleIdJSON(list: Person[]):void {
list.forEach(x => console.log(x.id))
}
那只是打印列表中每個對象的id,但是我如何按照上面的要求將輸出設為json?
謝謝分配
這里是 :
interface Person{
id: string;
}
function makePeopleIdJSON(list: Person[]) {
return {
personIds: list.map(x => x.id)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.