当前位置:网站首页>2022.7.29 Array
2022.7.29 Array
2022-07-31 07:19:00 【The secret to longevity is sleep.】
1. Basic operations of arrays
1. Definition: An array is a list object, and its prototype provides operations related to traversing and modifying elements. The length and element type of JS arrays are non-fixed (putting the data together is an array)
2. Declaration method:
//Literal form (commonly used, convenient)var list1 = [1,2,true,'hello']//constructor declarationvar list2 = new Array(1,2,true,'hello')3. Access elements of an array
var list = [1,2,3,4]console.log(list[2]) //Through the index (subscript), get 3var list = [1,2,3,4]console.log(list.length) //Get the length of the array, get 44. Query array subscript
var a = ["a", "b", "c"];var b = a.indexOf("c");console.log(b); // 2Second, the basic method of operating arrays, adding, deleting, modifying and checking
var list = [1, 2, 3, 4];// incrementlist[4] = 5; // [1, 2, 3, 4, 5]list[6] = 7; // [1, 2, 3, 4, 5, empty, 7]list.push(8); // [1, 2, 3, 4, 5, empty, 7, 8] add data at the endlist.unshift(0); // [0, 1, 2, 3, 4, 5, empty, 7, 8] add data to the front// changelist[2] = "a"; // [0, 1, 'a', 3, 4, 5, empty, 7, 8]// deletedelete list[2]; // [0, 1, Empty, 3, 4, 5, Empty, 7, 8] Generally not used, because it will produce Empty.with splicelist.pop(); // [0, 1, empty, 3, 4, 5, empty, 7] delete the last datalist.shift(); // [1, empty, 3, 4, 5, empty, 7] delete the first data// checkconsole.log(list[2]);Three, advanced array operation method
1.splice
Role: delete or replace elements; the function has a return value, which returns the deleted element; this method will change the original array.
Usage scenarios: replace elements in an array; delete part of an array; empty an array.
var list = ["a", "b", "c", "d", "e"];list.splice(0, 3, 2);console.log(list);The first parameter is to control the deletion or replacement from the first position (inclusive) (depending on whether the third parameter has a value), the second parameter is to control the number of deletions, and the third parameter will be deletedElements are replaced, separated by commas.
var list = ["a", "b", "c", "d", "e"];list.splice(0, 3, 2);console.log(list); //[2, 'd', 'e']If there is only one parameter, it means to delete all values after the first digit.
2.join
Function: Convert the data of the array type into a string, the difference from toString is that you can separate the elements with custom symbols.
var list = [1, 2, 3, 4, 5];var a = list.join("-");console.log(a); // 1-2-3-4-53.concat
Effect: Concatenates two or more arrays, does not change the existing array, but returns a new array containing the values of the concatenated arrays.
var a = [1, 2];var b = [3, 4];var ab = b.concat(a);console.log(ab); // [3, 4, 1, 2]边栏推荐
猜你喜欢

Analysis of the principle and implementation of waterfall flow layout

英语翻译软件-批量自动免费翻译软件支持三方接口翻译

Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)

机器学习反向传播的一些推导公式

NFS共享存储服务

银河麒麟高级服务器v10 sp1 手动加载Raid卡驱动

【 TA - frost Wolf _may - "one hundred plan" 】 art 2.3 hard surface

Obtaining server and client information

LeetCode刷题——摆动序列#376#Medium

搭建zabbix监控及邮件报警(超详细教学)
随机推荐
快速傅里叶变换(FFT)
深度解析 z-index
es6数组/数组对象求并集、交集、差集、去重、排序
项目练习——备忘录(增删改查)
04-SDRAM:读操作(突发)
什么是浮动?什么是文档流?清除浮动的几种方式及原理?什么是BFC,如何触发BFC,BFC的作用
Kubernetes调度
批量翻译软件免费【2022最新版】
How to use repeating-linear-gradient
(border-box)盒子模型w3c、IE的区别
单点登录 思维导图
Redux状态管理
CHI论文阅读(1)EmoGlass: an End-to-End AI-Enabled Wearable Platform for Enhancing Self-Awareness of Emoti
试题 历届真题 错误票据【第四届】【省赛】【B组】
Oracle 日期函数相关
Project exercise - memorandum (add, delete, modify, check)
uni-app生命周期
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
DHCP原理与配置
shell之条件语句(test、if、case)