Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an array with non duplicate values from multiple array

I need to go through multiple arrays and create one new array with all the values from the multiple arrays without duplicate, is there any plugins/quick way I can do this?

var x = {
  "12": [3, 4],
  "13": [3],
  "14": [1, 4]
};

The result should look something like this:

[1,3,4];
like image 361
Adam Avatar asked Jan 26 '18 15:01

Adam


1 Answers

You can do this using ES6 spread syntax and Object.values method.

var x = {
  "12": [3, 4],
  "13": [3],
  "14": [1, 4]
}

const result = [...new Set([].concat(...Object.values(x)))]
console.log(result)

Solution using Lodash

var x = {
  "12": [3, 4],
  "13": [3],
  "14": [1, 4]
}

const result = _.uniq(_.flatten(_.values(x)))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
like image 187
Nenad Vracar Avatar answered Nov 05 '22 03:11

Nenad Vracar