如何基于键过滤JSON,所以我只有所有对象的一种键类型列表

Tej*_*ngh 2 javascript arrays json google-sheets google-apps-script

[
  {
    "id":100,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
  ...
]
Run Code Online (Sandbox Code Playgroud)

这是我的示例对象。我是从url提取应用程序请求中获得的。我该如何过滤它,以便我可以仅发布一种类型的Value的列表。

例如:如果我想将其作为键ID进行过滤,我想得到一个类似于:100,101,...的列表。

谢谢

Yos*_*ero 5

而不是Array.prototype.filter()你应该使用Array.prototype.map()

码:

const data = [
  {
    "id":100,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
  {
    "id":101,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
]

const result = data.map(obj => obj.id)

console.log(result)
Run Code Online (Sandbox Code Playgroud)