Underscore.js-获取唯一的属性值 [英] Underscore.js - get unique property values

查看:124
本文介绍了Underscore.js-获取唯一的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我直到最近才发现underscore.js的功能,这对我恳求提出建议的方法仍然是新的:

I only recently discovered the power of underscore.js, still new to the methods I kindly ask for a suggestion:

我如何获得此信息:

[
    [{
        "name": "Type 2",
        "id": 14
    }],
    [{
        "name": "Type 1",
        "id": 13
    }, {
        "name": "Type 3",
        "id": 15
    }],
    [{
        "name": "Type 2",
        "id": 14
    }],
    [{
        "name": "Type 1",
        "id": 13
    }]
]

对此:

["Type 1","Type 2","Type 3"]

即没有重复且只有名称"属性.

i.e. no duplicated and "name" property only.

任何建议都值得赞赏.

推荐答案

_(data).chain().flatten().pluck('name').unique().value()

(将嵌套列表转换为一个平面列表,从列表中的每个对象中选择name,并使其唯一.)

(Convert the nested lists to a flat one, pick name from each of the objects in the list, and make it unique.)

这篇关于Underscore.js-获取唯一的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆