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
Table of contents Rendering API changes Render fu...
Today is 618, and all major shopping malls are ho...
1. Download the axios plugin cnpm install axios -...
When it comes to switching directories under Linu...
Copy code The code is as follows: <div class=&...
In this article, we’ll explore how async/await is...
Method 1: Use CSS overflow omission to solve The ...
This is the content of React 16. It is not the la...
The DIV floating effect (fixed position) is imple...
Nginx log description Through access logs, you ca...
The background color of the table can be set thro...
In practice, we often encounter a problem: how to...
Problem: vue-cil3 runs with warnings potentially ...
Front-end project packaging Find .env.production ...
Three functions: 1. Automatic vertical centering o...