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

相关推荐
夏幻灵2 小时前
JavaScript this 面试:五种绑定规则、优先级与常见面试陷阱
开发语言·javascript·面试
三天不学习3 小时前
Egg.js 4 突然爆火,原因是否归结于AI 原生落地需求爆发?
前端·javascript·全栈·egg.js
xcs194054 小时前
前端 vue 的前端页面debugger 进不去
前端·javascript·vue.js
信奥卷王5 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵5 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG6 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
码完就睡6 小时前
数据结构——红黑树
数据结构
码爸7 小时前
数据结构详细介绍
数据结构
Navigator_Z7 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode