当前位置:网站首页>JS学习笔记-- 数组方法 底层实现方式
JS学习笔记-- 数组方法 底层实现方式
2022-07-23 23:38:00 【(σ゚∀゚)σ..:**哎哟不错哦】
push – 尾部添加
返回数组的长度值
Array.prototype.myPush = function(val) {
if (arguments.length && arguments.length > 1) {
for (let i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i]
}
} else {
this[this.length] = val
}
return this.length
}
pop 尾部删除
只能删除一个 ,并返回删除的元素
// 尾部删除
Array.prototype.myPop = function() {
// 删除最后一个元素 并返回当前删除的元素
console.log('11==',this)
const val=this[this.length-1]
this.length--
return val
}
unshift 头部添加
返回数组长度
Array.prototype.myUnshift = function(val) {
const len = arguments.length
const len1 = this.length
if (len1) {
// 数组长度大于0,所有元素往后移动
if (len) {
for (let i = len1 - 1; i >= 0; i--) {
// const temp=this[i]
this[i + len] = this[i]
}
for (let i = 0; i < len; i++) {
this[i] = arguments[i]
}
}
} else {
// 数组长度等于0
if (len) {
for (let i = 0; i < len; i++) {
this[i] = arguments[i]
}
}
}
return this.length
}
shift 头部删除
无参数,返回当前删除的元素值
Array.prototype.myShift=function(){
for (var i = 0; i < this.length-1; i++) {
this[i]=this[i+1]
}
const val=this[0]
this.length--
return val
}
splice 删除 替换元素 插入元素
返回删除的元素 第一个参数 起始元素坐标,第二个元素 删除元素的个数,第三个至第N个 ,替换或新增的元素
持续更新中
边栏推荐
- Smart IOT source code with configuration IOT source code industrial IOT source code: support sensor analysis services, real-time data collection and remote control
- [数组]NC95 数组中的最长连续子序列-较难
- ret2shellcode
- Sql156 average completion rate of each video
- 怎么开户买收益6%的理财产品呢?
- bjdctf_2020_babystack
- Solidity-delegateCall插槽冲突分析与解决
- jarvisoj_level0
- 第六章、实现一个持久性适配器
- 1、 Simplification of digital logic
猜你喜欢

A great open source micro community light forum source code

JS convert numbers to uppercase

pwn1_sctf_2016

Arrayslist and sequence table -- Simulation Implementation

BGP基础实验

DDD思维架构学习

SQL语句实战学习

How to migrate databases in the flask framework

头插法创建链表并输出所有元素

Operating system not found solution after importing ISO into virtual machine
随机推荐
Kirin OS and Godson environment compilation and installation of greatsql
[array] longest continuous subsequence in nc95 array - difficult
cmake 编译工具小记
网上找客户经理办理基金开户安全吗??
Quickly learn to use file permissions
史上最全的2022年版Android面试题
ArraysList 与顺序表 ——模拟实现
Entropy weight method to optimize TOPSIS (matlab)
怎么开户买收益6%的理财产品呢?
DGS file upload
FreeRTOS personal notes - suspend / unhook tasks
Operating system not found solution after importing ISO into virtual machine
第五章、实现Web适配器
Galaxy Securities opens an account online. Is it safe to open an account on your mobile phone
DDD thinking structure learning
【Error】TypeError: expected str, bytes or os. PathLike object, not int
[SSM]前后台协议联调①
Go language multiple return values and return error types
Esp8266 nodemcu - get real-time weather from Suning API
PHP(2)