0

I have an array, that contains another arrays with string id's. I need to get an array with string ids, that are exists in all arrays of parent.

Here is example:

const parentArray = [ ['test1', 'test2', 'test3'], ['test2', 'test4', 'test5'], ['test3', 'test2', 'test6']];

//some magic. Maybe use lodash instersection func

console.log(result) => ['test2']

Important note! I don't know, how much arrays of elements in parent array, thats the problem that i cant understand how to use lodash intersection function:(

WhoIsDT
  • 695
  • 2
  • 9
  • 27
  • Does this answer your question? [Finding unique values in multiple arrays](https://stackoverflow.com/questions/35137255/finding-unique-values-in-multiple-arrays) – Expressingx Feb 07 '22 at 18:10
  • 1
    No! I need SAME elements in all multiply arrays, NOT unique! – WhoIsDT Feb 07 '22 at 18:14
  • ohh... my BAD :) – Expressingx Feb 07 '22 at 18:21
  • So you need to loop over the array. You make up a look up object or set and use us the string as the property and set it to one. You then loop over the next one and you add one if you saw it or set it to 1. When you are are at the end, you look for the counts that equal the length of the array. – epascarello Feb 07 '22 at 18:21

3 Answers3

1
const parentArray = [ ['test1', 'test2', 'test3'], ['test2', 'test4', 'test5'], ['test3', 'test2', 'test6']];

let common = parentArray.reduce((p,c) => p.filter(e => c.includes(e)));

// ['test2']
Expressingx
  • 1,480
  • 1
  • 14
  • 39
0

The below may be one possible way to achieve the desired result.

Code Sample

const commonElements = (arr1, arr2) => (
    arr1.length === 0
   ? arr2
   : [...(new Set([...arr1, ...arr2]))]
    .filter(
      x => arr1.includes(x) && arr2.includes(x)
    )
);

const intersectAllElements = (arr = parentArray) => (
    arr.reduce(
    (fin, itm) => (commonElements(fin, itm)),
    []
  )
);

Explanation

  • Set a helper-method named commonElements that will share either common elements between 2 arrays, or if the first array is empty then the second array
  • Iterate over the parentArray using .reduce
  • Initialize the aggregator fin as an empty array
  • For each item (which will be an array) in parentArray, track the common elements between the aggregator and given item

Code Snippet

const parentArray = [ ['test1', 'test2', 'test3'], ['test2', 'test4', 'test5'], ['test3', 'test2', 'test6']];

const commonElements = (arr1, arr2) => (
    arr1.length === 0
   ? arr2
   : [...(new Set([...arr1, ...arr2]))]
    .filter(
      x => arr1.includes(x) && arr2.includes(x)
    )
);

const intersectAllElements = (arr = parentArray) => (
    arr.reduce(
    (fin, itm) => (commonElements(fin, itm)),
    []
  )
);

console.log(intersectAllElements());
jsN00b
  • 3,584
  • 2
  • 8
  • 21
0

Lodash solution:

const parentArray=[["test1","test2","test3"],["test2","test4","test5"],["test3","test2","test6"]];

const result = parentArray.reduce((acc, arr) => _.intersection(acc, arr));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48