Datos:
[ { "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" } }, ]Cómo filtrar solo valores únicos,
uniqueValues = ["medal","trophy"]Lo intenté,
const uniqueTitles = new Set(games.category.title);const uniqueTitles = [...new Set(games.category.title)] //error mecanografiado. useEffect(() => { const uniqueTitles = games.filter((game:any) => { return new Set(game.category.title); }) setTitles(uniqueTitles); },[])Está utilizando Set como valor de retorno para una función de filtro. ¿Está realmente pensado de esa manera? Dados los datos:
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" } }, ]Puedes hacerlo:
const uniqueValues = new Set(); data.forEach(record => uniqueValues.add(record.rank._type)); console.log(uniqueValues);Aquí está el enlace .
Suponiendo que su matriz se llama data :
const unique = [...new Set(data.map(item => item.rank._type))];Solución similar a su primer intento:
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));