当前位置:网站首页>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]边栏推荐
猜你喜欢
随机推荐
测试 思维导图
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
(border-box) The difference between box model w3c and IE
One of the small practical projects - food alliance ordering system
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
浅析伪类和伪元素
芯塔电子斩获第十一届中国双创大赛芜湖赛区桂冠
MySQL的触发器
【Star项目】小帽飞机大战(七)
高并发与多线程之间的难点对比(容易混淆)
浅层了解欧拉函数
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
DHCP原理与配置
Moment.js常用方法
uni-app生命周期
Analysis of the principle and implementation of waterfall flow layout
Oracle 日期函数相关
Redux状态管理
数据库原理作业2 — JMU









