What is the most elegant way to implement an array of ES6 objects based on a property?

A solution that conforms to the meaning of the question

function unique(arr) {
    const res = new Map();
    return arr.filter((a) => !res.has(a) && res.set(a, 1))
}

because map key is unique, you can change the an in res.has (a) to whatever attribute you want to deduplicate, such as a.name

.

reference:

https://codeshelper.com/a/11.

Arrow function

return arr.filter((a) => !res.has(a) && res.set(a, 1))
//
return arr.filter(function(a){
    return !res.has(a) && res.set(a, 1);
});

1, the arrow function has a more concise syntax for writing code;
2, this will not be bound.

< hr >

Segmentation line, the following is my personal question, which is somewhat different from the question, and adopts the answer that meets my personal question. I"m sorry

that"s what I wrote.
referred to
https://codeshelper.com/q/10.

.

https://blog.csdn.net/zhihua_.

questions and articles, but I don"t feel very elegant. I don"t seem to use anything new in es6. Is there a better way to implement es6? Thank you

getGameVersionList() {
    const versionList = this.page.resultList
    const versionSet = [0]
    const hash = {}
    for (var i = 0, gameVersion; (gameVersion = versionList[i]) != null; iPP) {
      if (!hash[gameVersion.gameVersionNo]) {
        versionSet.push(gameVersion.gameVersionNo)
        hash[gameVersion.gameVersionNo] = true
      }
    }
    this.gameVersionNoSet = versionSet
  },
Mar.03,2021

getGameVersionList() {
    return [0, ...new Set(this.page.resultList.map(item => item.gameVersionNo))]
}

use filter. If you meet the requirements, you can directly return, the hash.

hash directly regards values as key. For example, [1, '1values, [1]] , these three values are all exactly the same as key ' 1' .
not even with map:

let map = new Map();
map.set([1], 'test');
map.get([1]) //undefined

before and after the [1] is not the same can not be obtained, or object simulation hash coordination type judgment.

< hr >

just thought about it, the two [1] are not duplicated in the first place. Depending on the requirements, you can still use map to do hash.


use lodash
identity" rel=" nofollow noreferrer "> unionBy

Menu