Anudeep Anudeep - 1 year ago 81
Javascript Question

How to collect all array unique values from array of objects using underscore?

I have an array of objects. Like

name : "A"
dept : ["a", "b", "c"]
name : "B"
dept : ["a", "e", "f", "g"]
name : "C"
dept : ["b", "c", "e", "j"]

Now I want to collect all unique array values of dept from this array of object using underscore.
The output should be save in a new array. Like

var newArray = ["a", "b", "c", "e", "f", "g", "j"]

I am new in underscore.js. Thanks in advance.

Answer Source
var project = [{
    name: "A",
    dept: ["a", "b", "c"]
}, {
    name: "B",
    dept: ["a", "e", "f", "g"]
}, {
    name: "C",
    dept: ["b", "c", "e", "j"]
}, ]

_UNIQUEAGEARRAY = _.flatten( {
    return item.dept


document.body.innerHTML = JSON.stringify(_UNIQUEAGEARRAY);

A Fiddle:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download