Ways to create a Set in JavaScript?

后端 未结 9 473
太阳男子
太阳男子 2021-01-31 07:06

In Eloquent JavaScript, Chapter 4, a set of values is created by creating an object and storing the values as property names, assigning arbitrary values (e.g. true) as property

9条回答
  •  攒了一身酷
    2021-01-31 07:33

    The first way is idiomatic JavaScript.

    Any time you want to store a key/value pair, you must use a JavaScript object. As for arrays, there are several problems:

    1. The index is a numerical value.

    2. No easy way to check to see if a value is in an array without looping through.

    3. A set doesn't allow duplicates. An array does.

提交回复
热议问题