Arrays are the most common data structures in the computer programming world. Any programming language contains arrays. It is a built-in model in a programming language that is usually very efficient and can be used to store data for different needs.
Arrays in JavaScript
The standard definition of an array is a linear collection of elements that can be accessed by an index, usually a number, to calculate the offset between the locations in which the elements are stored.
Whereas arrays in JavaScript, strictly speaking, should be called objects, are a special kind of object, and the indexes are its properties, which can be integers, but because the property names of objects in JavaScript must be strings, these indexes are internally converted from numeric to string types.
Use arrays
2.1 Creating and initializing arrays
You can declare an array variable with the [] operator:
var nums = []; // Length is 0
Copy the code
Of course, you can also declare an array by simply putting elements in it:
var nums = [1.2.3.4]; // Length 4
Copy the code
Alternatively, you can create it using the constructor:
var nums = new Array(1.2.3.4.5);
Copy the code
Similarly, you can use it to create an array of length 10 with zero elements:
var arr = new Array(10).fill(0);
Copy the code
Finally, we can determine whether an object is an Array by using array.isarray () :
var a = 1;
console.log(Array.isArray(a)); // false
console.log(Array.isArray(nums)); // true
Copy the code
2.2 Adding and removing elements
Add elements:
push
Method: add the element to the end of the array;unshift
Method: Insert an element at the beginning of an array.
Delete element:
pop
Method: Remove an element from the end of an array.shift
Method: Delete the first element of an array.slice
Method: Remove elements for the appropriate location and number. You can also add or replace elements with a third argument.
Tips!!! :
- In JavaScript, an array is an object that can be modified. Add elements, and it grows dynamically;
- through
push
和pop
Method, you can use an array to simulate a stack; - through
unshift
和shift
Method, you can use arrays to simulate queues.
2.3 Two-dimensional and multidimensional arrays
JavaScript only supports one-dimensional arrays, but you can implement any multidimensional array by nested arrays:
var arrs = [[1.2], [3.4]]
Copy the code
2.4 JavaScript array methods
The method name | describe |
---|---|
concat | Concatenate two or more arrays and return the result |
every | Runs the specified function for each element in the array, returning true if the function returns true for each item |
filter | Runs the specified function for each element in the array, returning an array of items for which the function returns true |
forEach | Runs the specified function for each element in the array, returning no value |
join | Concatenate all array elements into a single string |
indexOf | Returns the index of the first array element equal to the given argument, or -1 if not found |
lastIndexOf | Returns the largest index value found in the array for elements equal to the given parameter |
map | Runs the specified function for each item in the array, returning an array of the results of each function call |
reverse | Flip the array |
slice | Pass in the index value and return the elements in the array as the new array |
some | Run the specified function for each item in the array. Return true if any item returns true |
sort | Sort the elements of an array |
toString | Returns the array as a string |
valueOf | Similar to the toString |