当前位置:网站首页>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 4
4. Query array subscript
var a = ["a", "b", "c"];var b = a.indexOf("c");console.log(b); // 2
Second, 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-5
3.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]
边栏推荐
猜你喜欢
Database Principles Homework 2 — JMU
LeetCode brush # 376 # Medium - swing sequence
Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
Install and use uView
MySQL的触发器
关于求反三角函数的三角函数值
2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
搭建zabbix监控及邮件报警(超详细教学)
TCP/IP协议和互联网协议群
【云原生】3.3 Kubernetes 中间件部署实战
随机推荐
Markdown中的数学符号
How to choose a suitable UI component library in uni-app
【云原生】-Docker容器迁移Oracle到MySQL
SQLite数据库连接字符串
nohup原理
Kubernetes scheduling
gstreamer's caps event and new_segment event
DNS域名解析服务
Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
tidyverse笔记——tidyr包
codec2 BlockPool:不可读库
Shell编程规范与变量
gstreamer的caps event和new_segment event
Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
【编程题】【Scratch三级】2022.03 冬天下雪了
postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程
Database Principles Homework 2 — JMU
ls的用法
TypeScript编译(tsconfig.json)
银河麒麟高级服务器v10 sp1 手动加载Raid卡驱动