score:2

Accepted answer

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.

score: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));

score: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)

score:0

simple way:

array.from(new set(datalist.map(i => i.name)))

score:1

assuming your array is called data:

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

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


Related Query

More Query from same tag