How to find duplicate values ​​in a multidimensional array in angularjs

var arrayValues = [[2,3,5],[3,5]] var commonArrayValues = _.intersection(arrayValues); 

It currently works as

 _.intersection([[2,3,5],[3,5]]) Result: [2,3,5] 

But it should work like (the outer element must be removed)

 _.intersection([2,3,5],[3,5]) Expected Result: [3,5] 

Anyone will kindly provide me with the right solutions. Thank you in advance.

+5
source share
3 answers

You can use apply with intersection to get what you want:

 var result = _.intersection.apply(null, arrayValues); 

 var arrayValues = [[2,3,5],[3,5], [2,3,5,6]] var result = _.intersection.apply(null, arrayValues); document.getElementById('results').textContent = JSON.stringify(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore.js"></script> <pre id="results"></pre> 
+2
source

intersection * _. intersection (arrays)
Computes a list of values ​​that intersect all arrays. Each value in the result is present in each of the arrays.

_. intersection ([1, 2, 3], [101, 2, 1, 10], [2, 1]); => [1, 2]

 var arrayValues = [[2,3,5],[3,5]] 

Here arrayValues ​​is an array containing 2 arrays. Where, like _.intersection expects arrays as a parameter, not an array with arrays.

 _.intersection([2,3,5],[3,5]) 

or

 _.intersection(arrayValues[0],arrayValues[1]) 

will be displayed as what you need.

0
source

The only way I can think of is to use eval:

 var arrayValues = [[2,3,5],[3,5]] var evalString = '_.intersection('; arrayValues.forEach(function (element, index){ evalString += 'arrayValues['+index+'],'; }); evalString =evalString.slice(0, -1); evalString += ');' eval(evalString); 

evalString will end with something like _.intersection(arrayValues[0],arrayValues[1],...,arrayValues[n]);

0
source

Source: https://habr.com/ru/post/1239112/


All Articles