Remove duplicates from JSON array data

PHOTO EMBED

Sun Feb 11 2024 04:02:25 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #reacth #hook

// removes all duplicates of a key in a json array, in this case we create a new const that loops over a cities json array and checks for countries with the same name. it will return only the first instance of that country object where there are multiple of the same country

const countriesUnique = new Set(
  cities.map(city =>
    JSON.stringify({ country: city.country, emoji: city.emoji }),
  ),
)
const countries = [...countriesUnique].map(each => JSON.parse(each))
console.log(countries)
content_copyCOPY