3

Data:

[
  {
    "name": "Ankh of Anubis",
    "rank": {
      "_type": "medal",
      "current": "ankh-of-anubis"
    }
  },
  {
    "name": "Bonus Roulette",
    "rank": {
      "_type": "medal",
      "current": "bonus-roulette"
    }
  },
  {
    "name": "jetx",
    "rank": {
      "_type": "medal",
      "current": "jetx"
    }
  },
  {
    "name": "Gates of Olympus",
    "rank": {
      "_type": "trophy",
      "current": "gates-of-olympus"
    }
  },
]

How to filter only unique values,

uniqueValues = ["medal","trophy"]

I tried,

  1. const uniqueTitles = new Set(games.category.title);
  2. const uniqueTitles = [...new Set(games.category.title)] //typescript error.
 useEffect(() => {
    const uniqueTitles = games.filter((game:any) => {
      return new Set(game.category.title);
    })
    setTitles(uniqueTitles);
  },[])
Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69

5 Answers5

2

You are using Set as the return value for a filter function. Is it really intended that way? Given the data:

const data = [
  {
    "name": "Ankh of Anubis",
    "rank": {
      "_type": "medal",
      "current": "ankh-of-anubis"
    }
  },
  {
    "name": "Bonus Roulette",
    "rank": {
      "_type": "medal",
      "current": "bonus-roulette"
    }
  },
  {
    "name": "jetx",
    "rank": {
      "_type": "medal",
      "current": "jetx"
    }
  },
  {
    "name": "Gates of Olympus",
    "rank": {
      "_type": "trophy",
      "current": "gates-of-olympus"
    }
  },
]

You can do this:

const uniqueValues = new Set();
data.forEach(record => uniqueValues.add(record.rank._type));
console.log(uniqueValues);

Here's the link.

Charchit Kapoor
  • 8,934
  • 2
  • 8
  • 24
1

Assuming your array is called data:

const unique = [...new Set(data.map(item => item.rank._type))];

Credits: https://stackoverflow.com/a/58429784/6320971

BJZ
  • 36
  • 1
0

Solution similar to your first try :

const data = [{"name":"Ankh of Anubis","rank":{"_type":"medal","current":"ankh-of-anubis"}},{"name":"Bonus Roulette","rank":{"_type":"medal","current":"bonus-roulette"}},{"name":"jetx","rank":{"_type":"medal","current":"jetx"}},{"name":"Gates of Olympus","rank":{"_type":"trophy","current":"gates-of-olympus"}},];

const uniqueValues = new Set(data.map(elem => elem.rank._type));
uniqueValues.forEach(value => console.log(value));
AoooR
  • 370
  • 1
  • 9
0

const data = [
  {
    "name": "Ankh of Anubis",
    "rank": {
      "_type": "medal",
      "current": "ankh-of-anubis"
    }
  },
  {
    "name": "Bonus Roulette",
    "rank": {
      "_type": "medal",
      "current": "bonus-roulette"
    }
  },
  {
    "name": "jetx",
    "rank": {
      "_type": "medal",
      "current": "jetx"
    }
  },
  {
    "name": "Gates of Olympus",
    "rank": {
      "_type": "trophy",
      "current": "gates-of-olympus"
    }
  },
]

const result = data.filter((item, index) => {
  const itemIndex = data.findIndex(i => i.rank._type === item.rank._type)
  return itemIndex === index
})

console.log(result)
Mina
  • 14,386
  • 3
  • 13
  • 26
0

Simple way:

Array.from(new Set(dataList.map(i => i.name)))