Kalashri Aundhkar Kalashri Aundhkar - 1 year ago 84
Javascript Question

How to make union of nested array in lodash

I have below array format and i want to make union of it using lodash or normal js.

var testArray = [[1,2,3,4,5,6,7,8],[1,2,3,4,5,10,7,8],[1,2,3,6,7,8],[9],[3,4,5]]

I want to make union of all these into one and output should be below array.

testArray = [1,2,3,4,5,6,7,8,9,10]

Answer Source

You could combine flattenDeep with _.union. If needed apply sorting

var testArray = [[1,2,3,4,5,6,7,8],[1,2,3,4,5,10,7,8],[1,2,3,6,7,8],[9],[3,4,5]],
    result = _.chain(testArray)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

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