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

03-SDRAM:写操作(突发)

(border-box) The difference between box model w3c and IE

R——避免使用 col=0

Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model

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

银河麒麟服务器v10 sp2安装oracle19c

Install and use uView

测试 思维导图

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

【云原生】3.3 Kubernetes 中间件部署实战
随机推荐
gstreamer's caps event and new_segment event
shell的脚本的基本用法
leetcode 406. Queue Reconstruction by Height
What is float?What is document flow?Several ways and principles of clearing floats?What is BFC, how to trigger BFC, the role of BFC
进程和计划任务管理
TypeScript基本类型
How to use repeating-linear-gradient
ls的用法
运行 npm 会弹出询问 “你要如何打开这个文件?“
CHI论文阅读(1)EmoGlass: an End-to-End AI-Enabled Wearable Platform for Enhancing Self-Awareness of Emoti
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
One of the small practical projects - food alliance ordering system
Database Principles Homework 2 — JMU
Koa框架的基本使用
360推送-360推送工具-360批量推送工具
浅层了解欧拉函数
Analysis of pseudo-classes and pseudo-elements
04-SDRAM:读操作(突发)
【云原生】-Docker容器迁移Oracle到MySQL
Detailed explanation of js prototype