JavaScript数据结构&算法

JavaScript数据结构&算法

1 数据结构

1.1 数组Array

1.2 栈Stack

1.3 队列Queue

1.4 链表LinkedList

1.5 集合Set

js 复制代码
// new
let mySet = new Set();

// add
mySet.add(1);
mySet.add('hello world');
let o = { a: 1, b: 2 };
mySet.add(o); // add一个对象
mySet.add({ a: 1, b: 2 });

// has
const has = mySet.has(1);  // true
const has2 = mySet.has(3);  // false

// delete
mySet.delete(2);

// size
const size = mySet.size;

// 迭代1
for(let item of mySet) {
	console.log(item)
}
// 迭代2
for(let item of mySet.keys()) {
	console.log(item)
}
// 迭代3
for(let item of mySet.values()) {
	console.log(item)
}

// Set 转为 Array
const arr = [...mySet];
const arr2 = Array.from(set);

// Array 转为 Set
const set = new Set([1,3,5]);

// 交集 intersection
const intersection = new Set([...mySet].filter(x => set.has(x)));

// 差集 difference
const difference = new Set([...mySet].filter(x => !set.has(x)))

1.6 字典Map

js 复制代码
// 实例化
const map = new Map();

// 增
map.set('a','aa');
map.set('b', 'bb');

// 查key的存在
map.has('a'); // true
map.has('c'); // false

// 查value
map.get('a');
map.get('b');

// 删
map.delete('b');
// 清空
map.clear();

// 改:就是覆盖
map.set('a', 'aaaaa');

1.7 树Tree

1.8 图

1.9 堆Heap

2 算法

相关推荐
@HNUSTer8 分钟前
基于 HTML、CSS 和 JavaScript 的智能图像虚化系统
开发语言·前端·javascript·css·html
喜欢吃豆10 分钟前
LangGraph 深度解析(三):构建可观测、交互式 AI 智能体的流式架构权威指南
人工智能·python·算法·架构·大模型
三思而后行,慎承诺11 分钟前
requestIdleCallback 和 MessageChannel
javascript·reactjs
OEC小胖胖12 分钟前
React学习之路永无止境:下一步,去向何方?
前端·javascript·学习·react.js·前端框架·react·web
玉木子2 小时前
机器学习(六)朴素贝叶斯分类
开发语言·人工智能·python·算法·机器学习·分类
明月(Alioo)2 小时前
机器学习进阶,一文搞定模型选型!
人工智能·算法·机器学习
YL有搞头3 小时前
VUE的模版渲染过程
前端·javascript·vue.js·面试·模版渲染
Yyyy4823 小时前
test命令与参数
算法
耳总是一颗苹果5 小时前
数据结构---选择排序
c语言·数据结构·学习·算法·排序算法