0

I've been trying to find a way to remove duplicate items from an Array of Array but can't get my head around it. This is what I'm trying to do.

How do I make a single Array out of this and get rid of duplicates?

let input = [
  ['Apple', 'Banana'],
  ['Carrot', 'Peach'],
  ['Apple', 'Grapes'],
  ['Mango', 'Cherry'],
  ['Banana', 'Carrot'],
  ['Strawberry', 'Apple'],
  ['Peach', 'BlackBerry'],
  ['Orange', 'Apple'],
  ['Banana', 'Grapes']
]

let expectedOutput = ["Apples", "Carrots", "Banana", "Grapes", 
                     "Orange", "Peach", "Cherry", "Strawberry"];

So I found a way to do this by first using: let flattenedArr = input.flat(); followed by: let uniqueArray = [...new Set(flattnedLocationsArry)];

Amey079
  • 131
  • 7
  • 1
    The duplicates I've marked contain your answer. For future reference, if you'd like specific help please always include *what you've tried* in the question. SO is for getting help debugging existing code, not to write code for you. – Rory McCrossan Oct 07 '22 at 18:34
  • OMG thanks caTS. That just saved me. Worked like a charm. I first used the flat() method to flatten the Array and then used the Set() Constructor. Thank you. – Amey079 Oct 07 '22 at 18:39
  • Hey Rory sorry I'm new to StackOverflow my bad. – Amey079 Oct 07 '22 at 18:48

0 Answers0