【问题标题】:How to take a list of Objects and create a JSON of specific properties of the objects in the list?如何获取对象列表并创建列表中对象特定属性的 JSON?
【发布时间】:2016-08-01 23:14:05
【问题描述】:

我有一个如下所示的列表:

[ 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..?

感谢分配

【问题讨论】:

  • 你的意思是310394,244454...?否则我不确定你要做什么。

标签: javascript json typescript angular typescript1.8


【解决方案1】:

这里是:

interface Person{
    id: string;
}
function makePeopleIdJSON(list: Person[]) {
    return {
        personIds: list.map(x => x.id)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-15
    • 2020-11-22
    • 2014-10-17
    • 2017-06-29
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    相关资源
    最近更新 更多