We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
集合通常是由一组无序的, 不能重复的元素构成
add(value)
remove(value)
has(value)
clear()
size()
values()
//封装集合的构造函数 function Set() { // 使用一个对象来保存集合的元素 this.items = {} } //集合的操作方法 // 判断集合中是否有某个元素 Set.prototype.has = function (value) { return this.items.hasOwnProperty(value) } // 向集合中添加元素 Set.prototype.add = function (value) { // 1.判断集合中是否已经包含了该元素 if (this.has(value)) return false // 2.将元素添加到集合中 this.items[value] = value return true } // 从集合中删除某个元素 Set.prototype.remove = function (value) { // 1.判断集合中是否包含该元素 if (!this.has(value)) return false // 2.包含该元素, 那么将元素删除 delete this.items[value] return true } // 清空集合中所有的元素 Set.prototype.clear = function () { this.items = {} } // 获取集合的大小 Set.prototype.size = function () { return Object.keys(this.items).length /* 考虑兼容性问题, 使用下面的代码 var count = 0 for (var value in this.items) { if (this.items.hasOwnProperty(value)) { count++ } } return count */ } // 获取集合中所有的值 Set.prototype.values = function () { return Object.keys(this.items) /* 考虑兼容性问题, 使用下面的代码 var keys = [] for (var value in this.items) { keys.push(value) } return keys */ }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
集合结构
集合通常是由一组无序的, 不能重复的元素构成
操作方法
add(value)
:向集合添加一个新的项。remove(value)
:从集合移除一个值。has(value)
:如果值在集合中,返回true,否则返回false。clear()
:移除集合中的所有项。size()
:返回集合所包含元素的数量。与数组的length属性类似。values()
:返回一个包含集合中所有值的数组。实现
参考链接
The text was updated successfully, but these errors were encountered: