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

相关推荐
minstbe42 分钟前
半导体数据分析:GPR算法小白入门(三) 晶体管I-V特性仿真教程
算法
Restart-AHTCM1 小时前
前端核心框架vue之(路由篇3/5)
前端·javascript·vue.js
未知陨落1 小时前
LeetCode:60.单词搜索
算法·leetcode
mmz12072 小时前
动态规划 练习(c++)
c++·算法·动态规划
让时光到此为止。2 小时前
vue的首屏优化是怎么做的
前端·javascript·vue.js
tqs_123452 小时前
分sheet写入excel
开发语言·python·算法
西望云天2 小时前
基础组合计数(三道例题)
数据结构·算法·icpc
San302 小时前
JavaScript 流程控制与数组操作全解析:从条件判断到数据高效处理
javascript·面试·代码规范
dengzhenyue2 小时前
矩形碰撞检测
开发语言·前端·javascript
前端伪大叔3 小时前
第13篇:🎯 如何精准控制买入卖出价格?entry/exit\_pricing 实战配置
javascript·python