当前位置:网站首页>数组常用方法(2)
数组常用方法(2)
2022-07-24 18:08:00 【无围之解】
一、数组常用方法
1.push()
在尾部追加,类似于压栈,原数组会变。
const arr = [1, 2, 3]
arr.push(8)
console.log(arr) // [1, 2, 3, 8]
2.pop()
在尾部弹出,类似于出栈,原数组会变。数组的 push & pop 可以模拟常见数据结构之一:栈。
const arr = [1, 2, 3]
const popVal = arr.pop()
console.log(popVal) // 3
console.log(arr) // [1, 2]
// 数组模拟常见数据结构之一:栈
const stack = [0, 1]
stack.push(2) // 压栈
console.log(stack) // [0, 1, 2]
const popValue = stack.pop() // 出栈
console.log(popValue) // 2
console.log(stack) // [0, 1]
3.unshift()
在头部压入数据,类似于入队,原数组会变。
const arr = [1, 2, 3]
arr.unshift(0)
console.log(arr) // [0, 1, 2, 3]
4.shift()
在头部弹出数据,原数组会变。数组的 push(入队) & shift(出队) 可以模拟常见数据结构之一:队列。
const arr = [1, 2, 3]
const shiftVal = arr.shift()
console.log(shiftVal) // 1
console.log(arr) // [2, 3]
// 数组模拟常见数据结构之一:队列
const queue = [0, 1]
queue.push(2) // 入队
console.log(queue) // [0, 1, 2]
const shiftValue = qu边栏推荐
猜你喜欢

Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph

Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures

Emerging potential of interactive virtual reality technology in drug discovery

05mysql lock analysis

Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery

PXE efficient batch network installation

C language custom type explanation - structure

Win10 super good-looking mouse theme, you also try it

Use prometheus+grafana to monitor MySQL performance indicators

0625~<config>-<bus>
随机推荐
Blackmagic Fusion Studio 18
Three ways of redis cluster
Shanghai Jiaotong University team used joint deep learning to optimize metabonomics research
[OBS] cooperation between video and audio coding and RTMP transmission
mac数据库管理软件Navicat Premium Essentials Mac
1688/ Alibaba searches new product data by keyword API instructions
Guess JWT keyword
Interview assault 66: what is the difference between request forwarding and request redirection?
redis集群的三种方式
Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery
es(1)
C语言中的字符与字符串库函数的使用以及模拟实现
Quickly complete the unit test junit4 setting of intelij idea
Section 10 cache breakdown follow Daewoo redis ------- directory post
获取同程(艺龙)酒店数据
Simple test JS code
T245982 "kdoi-01" drunken flower Yin
700. Search DFS method in binary search tree
快速完成intelij idea的单元测试JUnit4设置
The use and Simulation of character and string library functions in C language