函数集合

  1. /*
  2. * @Author: yongyuan at <yongyuan253015@gmail.com>
  3. * @Date: 2021-08-01 22:59:12
  4. * @LastEditTime: 2021-08-01 23:39:16
  5. * @LastEditors: yongyuan at <yongyuan253015@gmail.com>
  6. * @Description: 函数实现集合
  7. * @FilePath: \JavaScript\collection\collection-function.js
  8. *
  9. */
  10. function Set() {
  11. let items = {};
  12. this.has = function (value) {
  13. return items.hasOwnProperty(value)
  14. }
  15. this.add = function (value) {
  16. if (!this.has(value)) {
  17. items[value] = value;
  18. return true;
  19. }
  20. return false;
  21. }
  22. this.remove = function (value) {
  23. if (this.has(value)) {
  24. delete items[value];
  25. return true;
  26. }
  27. return false
  28. }
  29. this.clear = function () {
  30. items = {}
  31. }
  32. this.size = function () {
  33. return Object.keys(items).length;
  34. }
  35. this.values = function () {
  36. return Object.values(items)
  37. }
  38. // 并集
  39. this.union = function (otherSet) {
  40. const unionSet = new Set();
  41. this.values().map((item) => {
  42. unionSet.add(item)
  43. })
  44. otherSet.values().map((item) => {
  45. unionSet.add(item)
  46. })
  47. return unionSet
  48. }
  49. // 交集
  50. this.intersection = function (otherSet) {
  51. const intersectionSet = new Set();
  52. const values = this.values();
  53. const otherValues = otherSet.values();
  54. let biggerSet = values;
  55. let smallerSet = otherValues;
  56. if (otherValues.length - values.length > 0) {
  57. biggerSet = otherValues;
  58. smallerSet = values;
  59. }
  60. smallerSet.map((item) => {
  61. if (biggerSet.includes(item)) {
  62. intersectionSet.add(item)
  63. }
  64. })
  65. return intersectionSet;
  66. }
  67. // 差集
  68. this.difference = function (otherSet) {
  69. const differenceSet = new Set();
  70. this.values().map((item) => {
  71. if (!otherSet.has(item)) {
  72. differenceSet.add(item);
  73. }
  74. });
  75. return differenceSet;
  76. }
  77. // 子集
  78. this.isSubsetOf = function (otherSet) {
  79. if (this.size() > otherSet.size()) {
  80. return false;
  81. }
  82. let isSubset = this.values().every((value) => otherSet.has(value));
  83. return isSubset
  84. }
  85. }
  86. const set = new Set();
  87. set.add(1);
  88. set.add(2);
  89. set.add(31);
  90. set.add(0);
  91. console.log(set.values());
  92. console.log(set.has(3));
  93. console.log(set.size());
  94. const setB = new Set();
  95. setB.add(10);
  96. setB.add(0);
  97. console.log(set.union(setB).values());
  98. console.log(set.intersection(setB).values());
  99. console.log(set.difference(setB).values());
  100. console.log(set.isSubsetOf(setB));

Class集合

  1. /*
  2. * @Author: yongyuan at <yongyuan253015@gmail.com>
  3. * @Date: 2021-07-30 23:21:43
  4. * @LastEditTime: 2021-07-31 18:06:52
  5. * @LastEditors: yongyuan at <yongyuan253015@gmail.com>
  6. * @Description: 集合的类声明
  7. * @FilePath: \JavaScript\collection\collection-class.js
  8. *
  9. */
  10. class Set {
  11. constructor() {
  12. this.items = {}; // 用对象来表示集合
  13. }
  14. has(element) {
  15. // Object原型有hasOwnProperty方法,它返回一个表明对象是否具有特定属性的布尔值
  16. // in 运算符返回的是对象在原型链上是否具有特定属性的布尔值
  17. return Object.prototype.hasOwnProperty.call(this.items, element)
  18. }
  19. // 往集合添加成员
  20. add(element) {
  21. if (!this.has(element)) {
  22. this.items[element] = element;
  23. return true;
  24. }
  25. return false;
  26. }
  27. // 删除集合成员
  28. delete(element) {
  29. if (this.has(element)) {
  30. delete this.items[element];
  31. return true;
  32. }
  33. return false;
  34. }
  35. // 清空集合成员
  36. clear() {
  37. return this.items = {}
  38. }
  39. // 返回集合大小
  40. size() {
  41. return Object.keys(this.items).length;
  42. }
  43. // 返回的集合成员的值
  44. values() {
  45. return Object.values(this.items)
  46. }
  47. // 集合运算:并集、交集、差集和子集
  48. union(otherSet) {
  49. const unionSet = new Set();
  50. this.values().forEach(value => {
  51. unionSet.add(value);
  52. });
  53. otherSet.values().forEach((value) => {
  54. unionSet.add(value);
  55. })
  56. return unionSet
  57. }
  58. // 交集
  59. intersection(otherSet) {
  60. const intersectionSet = new Set();
  61. const values = this.values();
  62. const otherValues = otherSet.values();
  63. let biggerSet = values;
  64. let smallerSet = otherValues;
  65. if (otherValues.length - values.length > 0) {
  66. biggerSet = otherValues;
  67. smallerSet = values;
  68. }
  69. smallerSet.forEach((value) => {
  70. if (biggerSet.includes(value)) {
  71. intersectionSet.add(value)
  72. }
  73. })
  74. return intersectionSet;
  75. }
  76. // 差集
  77. difference(otherSet) {
  78. const differenceSet = new Set();
  79. this.values().forEach(value => {
  80. if (!otherSet.has(value)) {
  81. differenceSet.add(value);
  82. }
  83. });
  84. return differenceSet;
  85. }
  86. // 子集
  87. isSubsetOf(otherSet) {
  88. if (this.size() > otherSet.size()) {
  89. return false
  90. }
  91. let isSubset = this.values().every(value => otherSet.has(value));
  92. return isSubset
  93. }
  94. }
  95. const set = new Set();
  96. set.add(1)
  97. console.log(set.values()); // [1]
  98. console.log(set.has(2));// false
  99. const setA = new Set();
  100. const setB = new Set();
  101. setA.add(1)
  102. setA.add(2)
  103. setB.add(1)
  104. setB.add(3)
  105. setB.add(2)
  106. const unionAB = setA.union(setB)
  107. console.log(setA)
  108. console.log(setB)
  109. console.log("并集", unionAB)
  110. const intersectionAB = setA.intersection(setB)
  111. console.log("交集", intersectionAB)
  112. const differenceAB = setB.difference(setA);
  113. console.log("差集", differenceAB)
  114. console.log("集合A是集合B的子集", setA.isSubsetOf(setB))
  115. console.log(new Set([{a:1,a:1,a:2,b:3}]))

ES6集合

  1. /*
  2. * @Author: yongyuan at <yongyuan253015@gmail.com>
  3. * @Date: 2021-08-01 23:59:34
  4. * @LastEditTime: 2021-08-02 23:35:42
  5. * @LastEditors: yongyuan at <yongyuan253015@gmail.com>
  6. * @Description: ES6的set结构实现集合
  7. * @FilePath: \JavaScript\collection\collection-ES6.js
  8. *
  9. * 数据结构set,类似数组,其成员值是唯一的,没有重复值。Set本身是一个构造函数。
  10. *
  11. * add()
  12. *
  13. * size
  14. *
  15. * has()
  16. *
  17. * delete()
  18. *
  19. * clear()
  20. *
  21. * 可以给数组去重,也可以给对象去重,去重相同属性。
  22. */
  23. const _set = new Set();
  24. _set.add({ name1: "duxin1", name2: "duxin2", name: "duxin3" })
  25. console.log(_set)
  26. console.log(_set.size)
  27. const _setB = new Set([1, 2, 3, 4, 3]);
  28. console.log("A集合", _set)
  29. console.log("B集合", _setB);
  30. //并集
  31. let union = new Set([..._set, ..._setB]);
  32. console.log("并集", union)
  33. // 交集
  34. let intersect = new Set([..._set].filter(value => _setB.has(value)))
  35. console.log("交集", intersect)
  36. // 差集
  37. const difference = new Set([..._set].filter(value => !_setB.has(value)));
  38. console.log("差集", difference)