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 算法

相关推荐
小O的算法实验室6 小时前
IEEE TASE,基于MPC的多无人机协同搜索竞争群体优化方法
算法
小飞学编程...7 小时前
【哈希表】
数据结构·哈希算法·散列表
Tbisnic7 小时前
BGE-M3 算法详解:从模型架构到三种检索方式的数学原理
算法·自然语言处理·大模型·bert·transformer·注意力机制
Brilliantwxx8 小时前
【算法从零到千】【55-58】哈希位图+常见数学运算 接口
算法
kyriewen8 小时前
我把最常踩的8个CORS跨域报错整理了一遍——第8个去年还不存在
前端·javascript
空堂与归8 小时前
用户分群找不到规律?用K-Means聚类算法自动发现数据模式
算法·机器学习·kmeans·聚类
动词ing9 小时前
【C语言】自定义函数+指针入门
c语言·开发语言·算法
重生之后端学习9 小时前
283. 移动零[简单]✅
开发语言·数据结构·算法·leetcode·职场和发展
一只小小的芙厨10 小时前
基础数论总结
笔记·学习·算法