在Json数组中解析Json数组并提取唯一值

时间:2017-05-18 19:22:38

标签: javascript arrays json underscore.js

我正在使用一个返回JSON响应的API,如下所示。

[
  [
    "Tab",
    "Service 1",
    172
  ],
  [
    "Admin",
    "Service 2",
    159
  ],
  [
    "Admin",
    "Service 3",
    5
  ],
  [
    "Admin",
    "Service 4",
    5
  ],
  [
    "Alerts",
    "Service 2",
    159
  ],
  [
    "Summary",
    "Service 1",
    99
  ],
  [
    "Import",
    "Service 2",
    71
  ],
  [
    "Covered",
    "Service 2",
    63
  ],
  [
    "Settings",
    "Service 2",
    53
  ],
  [
    "Application Config",
    "Service 2",
    51
  ]
]

我需要在javascript中从此列表中获取唯一服务。 预期回应:

["Service 1", "Service 2", "Service 3", "Service 4"]

我怎样才能做到这一点?我尝试使用underscorejs.org/#uniq,但没有帮助我。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

一种方法是在返回数组上使用map来获取您感兴趣的值,然后获取其中的唯一值。

let uniqueServices = _.uniq(apiResponse.map(r => r[1]))