当前位置:网站首页>js 对数组操作的 API 总结
js 对数组操作的 API 总结
2022-07-26 15:27:00 【一路向阳~负责的男人】
一、不改变原数组的方法
concat(): 连接数组,返回新的数组。- slice(): 截取数组,以索引为届,左闭右开。
- split(): 把一个字符串分割成字符串数组。如: ‘a&b&c’.split("&") ==> [a,b,c]
join(): 将数组内的元素用某个符号连接起来。如:[a,b,c].join("-") ==> “a-b-c”toString(): 直接把数组转为字符串格式,逗号分开。效果同 arr.join(",")ES6中的解构: newArr=[…a,…b],可以连接若干个数组,原数组不改变。
二、改变原数组的方法
原数组经过这些方法操作之后得到的新结果,即为原数组本身。
push(): 在数组末尾追加一个元素,返回数组长度。pop(): 在数组末尾删除一个元素,返回被去掉的元素。length末尾追加: 在数组末尾追加元素的另一种方法,即arr[arr.length],返回被添加的元素。unshift(): 在数组头部添加一个元素,返回添加的元素。shift(): 在数组头删除一个元素,返回被删除的元素。- splice(): 用于在指定位置添加、删除、替换元素。
(1)删除 arr.splice(index,len): 从索引index处(包括index,该方法都包括index本身,后面不赘述),删除长度为len个元素,返回被删除的元素组成的数组。若只写了index,没有第二个参数,则从index处开始删除后面所有元素。
(2)添加 arr.splice(index,0,...items): 从index处添加若干元素,返回[ ]空数组。(可以理解为被替换的元素是空)arr.splice(arr.length,0,…[1,2,3]): 连接数组,在末尾追加一个数组[1,2,3]给原数组。
(3)替换 arr.splice(index,len,...items): 从索引index开始,替换 len 个元素,用 items 去代替。若len超过了要替换的元素,则后面多余的全部被删除。返回被替换的元素组成的数组。 sort(): 排序方法,按字符串顺序升序排的。正确顺序(升序): arr.sort((a,b)=>a-b),当a-b返回负值时,函数会把a排序为比100更小的值。如 10-20= -10reverse(): 反转数组,颠倒原来的顺序,返回改变后的新数组。Array.prototype.push.apply(arr1,arr2): 在原型上连接数组,将arr2连接在arr1的尾部,参数只能为2个,多了无效。返回数组长度,arr1被改变,arr2不变。 arr1.push.apply(arr1,arr2) 效果相同。
文档持续维护,若有不妥之处,望大家指正或补充。
边栏推荐
- [leave some code] Apply transformer to target detection, and understand the model operation process of the model through debug
- 【留点代码】将transformer运用到目标检测上来,通过debug了解模型的模型运算流程
- 81.(cesium之家)cesium修改灰色背景(默认蓝色)
- Using two stacks to implement a queue
- Deep packet inspection using cuckoo filter paper summary
- pytorch---进阶篇(函数使用技巧/注意事项)
- LeetCode_ Prefix and_ Hash table_ Medium_ 525. Continuous array
- Tool skill learning (I): pre skills -makfile, make,.Mk
- 阿里巴巴一面 :十道经典面试题解析
- VS2019Debug模式太卡进不去断点
猜你喜欢

If you want to be good at work, you must first use its tools -c language expansion -- embedded C language (11)

PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (2)

Google tries to introduce password strength indicator for chromeos to improve online security

FOC电机控制基础

【DSCTF2022】pwn补题记录

OpenGL learning diary 2 - shaders

装备制造业的变革时代,SCM供应链管理系统如何赋能装备制造企业转型升级

Desktop application layout

# 工欲善其事必先利其器-C语言拓展--嵌入式C语言(十一)

数仓:爱奇艺数仓平台建设实践
随机推荐
2023 catering industry exhibition, China catering supply chain exhibition and Jiangxi catering Ingredients Exhibition were held in February
Zhaoqi science and technology innovation high-end talent project was introduced and implemented, mass entrepreneurship and innovation competition was organized, and online live roadshow was broadcast
R language uses LM function to build a multiple regression model with interactive terms, and uses step function to build a stepwise regression model to screen the best subset of predictive variables (
原来卡布奇诺信息安全协会是干这个的呀,一起来看看吧。
采购实用技巧,5个瓶颈物料的采购方法
Can the parameterized view get SQL with different rows according to the characteristics of the incoming parameters? For example, here I want to use the column in the transmission parameter @field
No module named ‘win32gui‘
【DSCTF2022】pwn补题记录
Practical purchasing skills, purchasing methods of five bottleneck materials
TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(3)
API 版本控制【 Eolink 翻译】
VS添加作者信息和时间信息的设置
University rankings in Beijing
Deep packet inspection using cuckoo filter paper summary
R language wilcox The test function compares whether there is a significant difference in the central position of the population of two nonparametric samples (if the two sample data are paired data, s
【基础】动态链接库/静态链接库的区别
Sword finger offer II 009. subarray with product less than k
[basic] the difference between dynamic link library and static link library
持续集成(一)基本概念简要介绍
If food manufacturing enterprises want to realize intelligent and collaborative supplier management, it is enough to choose SRM supplier system