js Set All In One
js Set All In One
交集、并集、补集、差集
function es5Set () {
this.items = [];
this.size = this.items.length;
this.has = function (value) {
if(this.items.includes(value)) {
return true;
} else {
return false;
}
};
this.add = function (value) {
if(!this.has(value)) {
this.items.push(value);
}
return this;
};
this.delete = function (value) {
const index = this.items.indexOf(value);
// const index = this.items.findIndex(item => item === value);
this.items.splice(index, 1);
};
this.clear = function () {
this.items = [];
};
this.values = function () {
return this.items;
};
this.keys = function () {
return this.values();
};
this.entries = function () {
const matrix = [];
for (const [index, item] of this.items.entries()) {
matrix.push([
index,
item,
]);
}
return matrix;
};
this.forEach = function (callbackFn, thisArg) {
for (const item of this.items) {
const obj = {
value: item,
key: item,
set: this,
};
callbackFn(obj, this.arguments);
}
};
// TODO: 交集、并集、补集、差集 (ES6 static 方法 / ES5 prototype 方法)
this.prototype.intersection = function () {
//
};
this.union = function () {
//
};
this.complement = function () {
//
};
this.difference = function () {
//
};
}
es5Set.prototype.intersection = function () {
//
};
MDN Set
Set forEach
The forEach() method executes a provided function once for each value in the Set object, in insertion order.
Set.prototype.forEach()
const log = console.log;
const set = new Set(['foo', 'bar', undefined]);
const es6Log = (value, key, set) => {
log('\n');
log('ES6: value, key, set =', value, key, set);
log('ES6: this.arguments', this.arguments);
};
set.forEach(es6Log, this.arguments);
log('\n\n');
function es5Log(value, key, set) {
log('\n');
log('ES5: value, key, set =', value, key, set);
log('ES5: this.arguments', this.arguments);
}
set.forEach(es5Log, this.arguments);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/forEach
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
refs
https://githubhelp.com/loiane/javascript-datastructures-algorithms
https://github.com/loiane/javascript-datastructures-algorithms
https://github.com/loiane/javascript-datastructures-algorithms/tree/main/test/js
https://github.com/loiane/javascript-datastructures-algorithms/tree/main/test/ts
https://github.com/loiane/javascript-datastructures-algorithms/tree/main/test/ts/data-structures
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15838321.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-01-24 css background transparent All In One
2021-01-24 vue template All In One
2021-01-24 css position sticky All In One
2020-01-24 sketch 导出 svg
2019-01-24 toast components
2019-01-24 html template & iframe & CORS All In One
2019-01-24 how to know iframe is loaded in js