PrefaceArrays are a special kind of object. There is no real array in js, it just uses objects to simulate arrays. The same method for viewing object properties also applies to arrays. It is worth noting that the array subscript type is a string, not a number. The difference between typical arrays and js arrays Typical array features similar to C/C++ are as follows:
js array
The above figure illustrates the last point: an array can have any key. To prove that the subscript of an array is a string, use Creating an ArrayThere are two ways to create an array: let arr = [1,2,3] let arr = new Array(1,2,3) Convert string to array split() Array.from() There are two ways to convert a string into a string array. Pseudo-arrayAn array without common attributes of an array is a pseudo-array (there is no prototype of an array in the prototype chain of a pseudo-array) There are no push, pop and other methods in the pseudo array (as can be seen by After the conversion, you can push successfully. Merge two arrays
This method does not change the original array. let arr1 = [1,2,3] let arr2 = [4,5,6] arr1.concat(arr2) // [1,2,3,4,5,6] arr1//[1,2,3] arr2//[4,5,6] Intercepting an Array
This method does not change the original array. let arr = [1,2,3,4,5,6] arr.slice(3) //[4,5,6] arr//[1,2,3,4,5,6] Deleting array elements Delete the head element: let arr = [1,2,3,4,5,6] arr.shift() // 1 arr//[2, 3, 4, 5, 6] Delete the tail element: let arr = [1,2,3,4,5,6] arr.shift() // 6 arr//[1, 2, 3, 4, 5] Delete the middle: let arr = [1,2,3,4,5,6,7,8,9] //Delete element 4 arr.splice(3,1)//4 arr//[1, 2, 3, 5, 6, 7, 8, 9] let arr = [1, 2, 3, 5, 6, 7, 8, 9] //Delete subscript 3 and add 3.5 and 4 arr.splice(2,1,3.5,4) // 3 arr//[1, 2, 3.5, 4, 6, 7, 8, 9] View array elementsView Properties
let arr = [1,2,3,4,5] arr.x='xxx' Object.keys(arr) //["0", "1", "2", "3", "4", "x"] Object.values(arr) // [1, 2, 3, 4, 5, "xxx"]
View elements containing only numbers Check if an element is in the array let arr = [1,2,3,4,5,6] arr.indexOf(2) // 1 arr.indexOf(7) // 0 Finding elements using conditions //Find the first even element let arr=[1,2,3,4,5,6] arr.find(item=>item%2===0)//2 //Find the first even subscript let arr=[1,2,3,4,5,6] arr.findIndex(item=>item%2===0)//1 Add elements to an array Add at the end: let arr = [3,4,5,6] arr.push(7,8,9) arr//[3,4,5,6,7,8,9] Header addition: let arr = [3,4,5,6] arr.unshift(1,2,3) arr//[1, 2, 3, 3, 4, 5, 6] Add in the middle: let arr = [1,2,3,4,5,6,7] //Add 3.33,3.44 to the position with subscript 2 arr.splice(2,0,3.33,3.44) // [1, 2, 3.33, 3.44, 3, 4, 5, 6, 7] SummarizeThis concludes this article on the basic usage summary of js arrays. For more relevant js array usage content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL 5.7.17 winx64 installation and configuration method graphic tutorial
>>: How to configure MySQL on Ubuntu 16.04 server and enable remote connection
Original article: Ultimate IE6 Cheatsheet: How To...
1. Initialize data DROP TABLE IF EXISTS `test_01`...
1. Promise description Promise is a standard buil...
This article will examine the ES6 for ... of loop...
Preface Due to the needs of the company's bus...
With the popularization of 3G, more and more peop...
In the previous blog, we learned about the usage ...
Implementing process analysis (1) How to call rep...
Deploy database based on docker sudo docker pull ...
Table of contents 1. Use of arrow functions 1. Fr...
1. SSH remote management SSH Definition SSH (Secu...
1. Log in to VPN using IE browser 2. Remote login...
Cerebro is an evolution of the Elasticsearch Kopf...
Dynamic rem 1. First, let’s introduce the current...
Ellipses appear when multi-line text overflows Th...