当前位置:网站首页>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]边栏推荐
猜你喜欢

关于求反三角函数的三角函数值

R——避免使用 col=0

Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software

浅析伪类和伪元素

批量翻译软件免费【2022最新版】

单点登录 思维导图

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

芯塔电子斩获第十一届中国双创大赛芜湖赛区桂冠

2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)

One of the small practical projects - food alliance ordering system
随机推荐
2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
磁盘管理与文件系统
Third-party library-store
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
gstreamer的caps event和new_segment event
试题 历届真题 错误票据【第四届】【省赛】【B组】
一文读懂 MongoDB 和 MySQL 的差异
【Star项目】小帽飞机大战(八)
Redux状态管理
Dart入门
第十七章:回溯探求指定入口的马步遍历,贪心无回溯探求马步遍历,递归探求nxm棋盘带障碍马步遍历
4-1-7 二叉树及其遍历 家谱处理 (30 分)
MySQL笔记下
(border-box) The difference between box model w3c and IE
银河麒麟高级服务器v10 sp1 手动加载Raid卡驱动
Analysis of pseudo-classes and pseudo-elements
项目练习——备忘录(增删改查)
2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition