2

I have this returned group of arrays returned as in, which is output console logging an array named forTypes:

 ["milk", "bread"]
 ["milk"]
 ["bread"]
 ["cheese", "jam"]

I have been trying to filter its out using

let forTypesUnique = [...new Set(forTypes)];

I have also tried using other functions such as Reduce() but no luck!

Should I combine those separate arrays into one and then filter out? Any suggestions would be appreciated.

11
  • to prevent duplicates you first need to concate all arrays to single one, and only after create a set. you also can use "flat" Commented Mar 11, 2020 at 20:34
  • The issue is that the API returns them as such, not even indexed - so I am not sure what to use to access them. Commented Mar 11, 2020 at 20:36
  • 1
    if you are allowed to use libraries, then use lodash's flatten to flatten the nested arrays into a single array of values, which you can then pass to your Set constructor to de-dupe. If this is some learning exercise where you cannot use libraries, then try looping through the array, then checking if the value you have is also an array, and looping through it...in your loop add items to the set one by one. Commented Mar 11, 2020 at 20:37
  • @Zeusox forTypes is array of arrays? Commented Mar 11, 2020 at 20:38
  • @Brandon I am not allowed to use libraries for that. @ Captain Mhmdrz_A it is not being wrapped into an array or object. Commented Mar 11, 2020 at 20:39

3 Answers 3

2

You can use flat and Set:

const forTypes = [
    ["milk", "bread"],
    ["milk"],
    ["bread"],
    ["cheese", "jam"]
];
const flatForTypes = forTypes.flat()
const forTypesUnique = [...new Set(flatForTypes)];

console.log(forTypesUnique)

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

The issue they do not have a parent array wrapping them up!
1

If all the arrays are come from server as nested array you can do the following:

let arr= [
 ["milk", "bread"],
 ["milk"],
 ["bread"],
 ["cheese", "jam"]
]
let f = arr.flat()
console.log(f);// ["milk", "bread", "milk", "bread", "cheese", "jam"]
let result = [...new Set(f)]


console.log(result)// ["milk", "bread", "cheese", "jam"]

3 Comments

The issue they do not have a parent array wrapping them up!
but may be you can warp them?
I was thinking about the same thing
1

Before create set you have to join arrays.

const typesGroup = [
    ['milk', 'bread'],
    ['milk'],
    ['bread'],
    ['cheese', 'jam'],
];

const forTypes = [].concat(...typesGroup);
const forTypesUnique = [...new Set(forTypes)];

instead of concat you can use flat function, but it is in stage 3 proposal for now

const forTypes = typesGroup.flat();

UPDATED:

if you don't have wrapper array, then you probably have any kind of function which you call to process items, so you can use closure:

const setOfTypes = new Set();

...

const updateForTypesList = (forTypes) => {
    forTypes.map(type => setOfTypes.add(type));
    return [...setOfTypes];
}

...

const resultList = updateForTypesList(['milk', 'bread']);

1 Comment

The issue they do not have a parent array wrapping them up!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.