“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”
An array of
Know the array
Arrays are our old friends. In general, arrays are used to store values of the same data type, for example, an array has a list of objects in memory, a list of strings, a list of values, etc. In JS, arrays can store different types of values, but this is not recommended
In addition, the physical addresses of the array are adjacent, that is, the addresses stored in the computer.
Create an array
New is often used when JS creates arrays. We can also assign values to arrays at creation time
var ary = new Array('1'.'2')
Copy the code
And of course the most common and recommended creation method
var ary = ['1'.'2']
Copy the code
Array traversal
If you want to find an element in an array or if you want to know the length of an array, the most common thing we do is walk through an array
Probably the most common traversal we do is to use a for loop to traverse an array
for (var i = 0; i < ary.length; i++) {
alert(ary[i])
}
Copy the code
However, some iteration methods of arrays have been encapsulated in JS to make array traversal faster
-
ForEach: Cannot be terminated in the middle of a procedure; you need to traverse each element to find it.
const arr = ['我'.'you'.'he'] arr.forEach((item,index) = > { if(item === 'you') { console.log(index) } }) Copy the code
-
Some: The some loop can be terminated by retrun true after the corresponding item is found
const arr = ['我'.'you'.'he'] arr.some((item,index) = > { if(item === "You") { return true; // Terminate the loop}})Copy the code
-
Every: Returns the Boolean value of whether each item satisfies the judgment condition
const arr = [
{id:1.name:'clothes'.state:true},
{id:2.name:'fruit'.state:true},
{id:3.name:'computer'.state:true}]const result = arr.every(item= > item.state == true)
Copy the code
Array capacity
Normally speaking, the capacity of the array in common languages can not be automatically changed, we need to expand the operation, expanding the array in JS is relatively simple
ary.length = 10
Copy the code
Or we can just add elements
ary[ary.length] = 9
Copy the code
Or use the push method to add elements to the end of the array
numbers.push(11)
Copy the code
But if we’re comparing low-level arrays we need to do some complicated things
If we want to increase the array is the need to speak to copy an array within an array of length is longer, the linked list and array is so so, the difference between a linked list can be infinite expansion, but an array, the array of expansion is a time-consuming, enter oneself for an examination, delete and add, because and expansion is essentially the same, but relative to the chain table lookup or fast, You can do this by using subscripts directly.
Increase for deletion
-
Adding an element: Adding an element is adding an element. We’ve already mentioned expansion, but what if we want to add an element to the middle of an array?
var ary =['1'.'2'.'3'.'4'] Copy the code
Here we want to add an element between 2 and 3, what do we do?
for (var i = ary.length+1; i = 3; i--) { ary[i] = ary[i-1] } ary[3] = '5' Copy the code
The idea is that we can use a for loop to move the position of the element after 2 backwards, starting at the end, stopping at 3 and assigning it directly to the array subscript to add and insert at any position
-
Remove elements
Pop () is used to remove the element at the end of the array
var ary =['1'.'2'.'3'.'4'] ary.pop() console.log(ary) Copy the code
But if we want to delete an element anywhere in the array the same way we would add an element anywhere in the array
var delet = 2 for (var i = delet; i < ary.length; i++) { ary[i] = ary[i+1] } ary.pop() Copy the code
You can declare the subscript of the array you want to delete, then start at the target location, let the subscript element overwrite the previous element, and finally use pop() to delete the element at any location
-
Changing an element anywhere in the array and getting an element anywhere in the array just use subscripts!
ary[1] = '3' console.log(ary[0]) Copy the code