Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore.js - get unique property values

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

How do I get from this:

[
    [{
        "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
    }]
]

to this:

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

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

Any suggestions much appreciated.

like image 397
Iladarsda Avatar asked Feb 04 '14 16:02

Iladarsda


1 Answers

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

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

like image 142
Jakub Roztocil Avatar answered Oct 19 '22 18:10

Jakub Roztocil