0

I have this arrray of arrays: [ [ 'RF1', ' FF1' ], [ 'FF1' ], [ 'FF1' ] ]

I am trying to destructure and reduce down to unique values ['FF1', 'RF1'] (order doesn't matter).

I tried something I found here, with the following function.

const merge = (...arrays) => {
    let jointArray = [];
    arrays.forEach(arr => {
        jointArray = [...jointArray, ...arr];
    });
    console.log('jointArray', jointArray);
    const unique = jointArray.filter((item, index) => jointArray.indexOf(item) === index);
    console.log('unique', unique);
    return unique;
}

let arr = [ [ 'RF1', ' FF1' ], [ 'FF1' ], [ 'FF1' ] ];
merge(arr);

and it returns exactly what I am putting in, what am I missing here?

jointArray [ [ 'RF1', ' FF1' ], [ 'FF1' ], [ 'FF1' ] ]
unique [ [ 'RF1', ' FF1' ], [ 'FF1' ], [ 'FF1' ] ]
shaun
  • 1,031
  • 12
  • 39
  • 3
    basically, it's `new Set(a.flat())` in modern javascript – georg Sep 11 '19 at 17:53
  • @georg: yes, perhaps wrapped in `[... ]`. But only because the values are primitives. If you need value equality for a non-primitive, the `Set` won't help. – Scott Sauyet Sep 11 '19 at 17:56
  • 1
    @ScottSauyet what about this question suggests to you that non-primitives are involved...? – Patrick Roberts Sep 11 '19 at 17:57
  • @PatrickRoberts: Nothing in particular, but when some people post a question, they often simplify the data. They might have taken their User or whatever objects and made them strings to make it easier to ask briefly. This is encouraged, in fact. Then they would have to come back to ask why it still doesn't work with the relevant data. That may not at all be the case here, but I've seen several such conversations involving `Set` and `Map` instances. – Scott Sauyet Sep 11 '19 at 18:02
  • 2
    @shaun: Others are giving you perfectly good alternatives. But as to why that isn't working for you, it's because that version of `merge` takes separate arguments rather than an array of them. If you remove `...` from the parameter it should just work: `const merge = (...arrays) => { ... }` => `const merge = (arrays) => { ... }`. – Scott Sauyet Sep 11 '19 at 18:04
  • Check out my answer which doesn't use `flat()`: https://stackoverflow.com/a/57896833/2924577. – Nikhil Sep 11 '19 at 21:14
  • If you think any of the responses has answered your question, then consider marking it as accepted for the benefit of future readers. More info: https://stackoverflow.com/help/someone-answers. – Nikhil Sep 16 '19 at 20:21

3 Answers3

1

You can flat array and than use set

let arr = [
  ['RF1', ' FF1'],
  ['FF1'],
  ['FF1']
]

let flaten = arr.flat()
let newSet = new Set(flaten)

console.log([...newSet])

If you want to treat the leading and trailing space insignificant you can loop and trim after flattening the array

let arr = [
  ['RF1', ' FF1'],
  ['FF1'],
  ['FF1']
]

let flaten = arr.flat().map(a => a.trim())
let newSet = new Set(flaten)

console.log([...newSet])
Code Maniac
  • 33,907
  • 4
  • 28
  • 50
  • I am running node v10.15.3, I just keep getting `.flat is not a function` anything more I can do? – shaun Sep 11 '19 at 19:10
  • @shaun may this version don't support `flat`, you can use alternatives [`Merge/flatten an array of arrays`](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays) – Code Maniac Sep 11 '19 at 19:12
0

You can iterate over all the elements and add them to a Set, and then convert to an array using Array.from().

var data = [ [ 'RF1', ' FF1' ], [ 'FF1' ], [ 'FF1' ] ];

var unique = new Set();

data.forEach(arr => {
  arr.forEach(value => {
    unique.add(value.trim());
  });
});

var result = Array.from(unique);

console.log(result);
Nikhil
  • 5,957
  • 9
  • 26
  • 55
-1
var arr = [ [ 'RF1', ' FF1' ], [ 'FF1' ], [ 'FF1' ] ]
var myArray = arr.flat(Infinity);;
var uniq = [...new Set(myArray)];
Rahul Beniwal
  • 342
  • 1
  • 8