2

I have an array of objects. Like

    project=[
        {
            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.

1
  • You can flatten the all arrays dept and get the _uniq Commented Oct 21, 2016 at 12:17

7 Answers 7

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



_UNIQUEAGEARRAY = _.flatten(project.map(function(item) {
    return item.dept
}));

console.log(_.uniq(_UNIQUEAGEARRAY));

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

A Fiddle:

http://jsfiddle.net/7k3ewb49/

Sign up to request clarification or add additional context in comments.

1 Comment

Goodness. Exactly what I want. Thanks a lot Thalaivar.
1

This should do it:

_.uniq(_.flatten(project.map(x => x.dept)));

Comments

1

You can use Set and reduce() to do this in pure js.

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

var result = [...new Set(project.reduce((r, e) => r.concat(e.dept), []))]
console.log(result)

Comments

1

Here's a solution using underscore's pluck, flatten and uniq:

var result = _.chain(project)
    .pluck('dept')
    .flatten()
    .uniq()
    .value();

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

    var result = _.chain(project)
    	.pluck('dept')
    	.flatten()
    	.uniq()
    	.value();
        
document.getElementById('result').textContent = JSON.stringify(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

<p>
  <pre id="result"></pre>
</p>

Comments

0

Try This.

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

var depts = [];

projects.map((project) => {
  depts = depts.concat(project.dept)
});

var result = depts.filter((item, pos) => depts.indexOf(item) == pos);

console.log(result);

Comments

0

ES2015 version:

const newArray = project.reduce((res, {dept}) => 
    (dept.forEach(v => !res.some(r => r === v) && res.push(v)), res), []);

also

const newArray = [...new Set(Array.prototype.concat.apply([], project.map(({dept}) => dept)))]

Comments

0

You could use spread syntax ... and Set

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

var result = project.reduce((r, a) => [...new Set([...r, ...a.dept])], []);
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.