我正在使用一个返回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,但没有帮助我。任何人都可以帮忙吗?
答案 0 :(得分:0)
一种方法是在返回数组上使用map
来获取您感兴趣的值,然后获取其中的唯一值。
let uniqueServices = _.uniq(apiResponse.map(r => r[1]))