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

相关推荐
阿豪学编程3 小时前
LeetCode724.:寻找数组的中心下标
算法·leetcode
墨韵流芳3 小时前
CCF-CSP第41次认证第三题——进程通信
c++·人工智能·算法·机器学习·csp·ccf
终端鹿4 小时前
Vue3 模板引用 (ref):操作 DOM 与子组件实例 从入门到精通
前端·javascript·vue.js
csdn_aspnet4 小时前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
凌波粒4 小时前
LeetCode--349.两个数组的交集(哈希表)
java·算法·leetcode·散列表
蜡台5 小时前
Vue 打包优化
前端·javascript·vue.js·vite·vue-cli
卷帘依旧5 小时前
JavaScript中this绑定问题详解
前端·javascript
paeamecium6 小时前
【PAT甲级真题】- Student List for Course (25)
数据结构·c++·算法·list·pat考试
Book思议-6 小时前
【数据结构】栈与队列全方位对比 + C 语言完整实现
c语言·数据结构·算法··队列
SteveSenna6 小时前
项目:Trossen Arm MuJoCo
人工智能·学习·算法