The basic concept
- What is an array? Arrays are just for storageA set ofData stuffPay attention to the point:
- Different from number/ string/ Boolean/null/ undefined (basic data type)
- Arrays are not base data types, they are reference data types (object types)
- How do I create an array? Let variable name = new Array(size);
- How tooperationdata
- Store data variable name [index number] = Data to store;
- Get the stored data variable name [index number];
Pay attention to the point
-
Unlike other programming languages, which store junk data or 0 by default, if there is no data stored in the index of the array
-
Unlike other programming languages, JavaScript does not report an error when accessing an index that does not exist in an array, and returns undefined. Other programming languages report an error or return dirty data once the index is out of range
-
Unlike other programming languages, arrays automatically expand when they run out of storage space in JavaScript, where arrays are fixed in size
-
Unlike other programming languages, JavaScript arrays can store different types of data. In other programming languages arrays can only store the same type of data (either all strings, all numbers, etc.)
-
Unlike in other programming languages, arrays in JavaScript don’t have to be allocated contiguously, they’re allocated in a hash map. What is a hash map? Just as a dictionary can find the corresponding character by its radical, we can find the corresponding space by its index
Array allocation has also been optimized in browsers
- If all the data is of the same type, contiguous storage space is allocated as much as possible
- If the data types are not the same, no contiguous storage space is allocated
-
The way an array is created
-
Create arrays using constructors
letVariable name =new Array(size); // Create an array of the specified size letVariable name =new Array(a);// Create an empty array letVariable name =new Array(data1, data2, ...) ;// Create an array with data Copy the code
-
Create arrays with literals (arrays are usually created with literals in development)
letVariable name = [];// Create an empty array letVariable name = [data1, data2... ;// Create an array with data Copy the code
-
Array traversal and destruct assignment
traverse
Array traversal is the process of retrieving all the data stored in the array
for(let i = 0; i < arr.length; i++){
console.log(arr[i]);
}
Copy the code
Deconstruction assignment
Deconstructing assignment is a new method of assignment in ES6
/ / ES6 before
let arr = [1.3.5];
let a = arr[0];
let b = arr[1];
let c = arr[2];
Copy the code
// ES6 new mode
let [a, b, c] = arr;
console.log("a = " + a);
console.log("b = " + b);
console.log("c = " + c);
Copy the code
Note:
-
In a deconstructed assignment of an array, the format to the left of the assignment number must be exactly the same as the format to the right of the assignment number in order to be fully deconstructed
-
In array deconstruction assignment, the number on the left can be different from the number on the right. The number on the left will be lost, and the value on the right will be empty
-
In an array deconstruction assignment, we can specify a default value for the left side, and a default value for the left side when the right side is small
let [a, b = Awesome!, c = 888] = [1]; Copy the code
-
In a deconstructed assignment of an array, the default value is overwritten if the left-hand pit has a corresponding right-hand value
-
You can also use the new extension operators in ES6 to wrap the remaining data in destructively assigned arrays
-
If an extension operator is used in a deconstructed assignment of an array, the extension operator must be written last
Supplement:
New extension operators in ES6:…
// let [a, b] = [1, 3, 5];
let [a, ...b] = [1.3.5];
console.log("a = " + a);
console.log(b);
Copy the code
Array add, delete, change, check
To add a push unshift
Requirement: Add a push to the end of the array
let arr = ["a"."b"."c"];
arr[3] = "d";
console.log(arr);
Copy the code
// The push method adds a new piece of data to the end of the array and returns the current length of the array
let arr = ["a"."b"."c"];
let res = arr.push("d");
console.log(res);
console.log(arr);
Copy the code
Requirement: Add two pieces of data to the end of the array (increment)
// Rookie behavior
let arr = ["a"."b"."c"];
arr.push("d");
arr.push("e");
console.log(arr);
Copy the code
// Array push methods can take one or more arguments
let arr = ["a"."b"."c"];
arr.push("d"."e");
console.log(arr);
Copy the code
Requirement: Add unshift to the front of the array
let arr = ["a"."b"."c"];
// arr[-1] = "m"; // ["a", "b", "c", -1: "m"]
// The unshift method, like the push method, returns the length of the current array after the addition
let res = arr.unshift("m");
console.log(res);
console.log(arr);
Copy the code
Requirement: Add two data items to the front of the array (increment)
// Rookie behavior
let arr = ["a"."b"."c"];
arr.unshift("m");
arr.unshift("w");
console.log(arr);
Copy the code
The unshift method, like the push method, can take one or more arguments
let arr = ["a"."b"."c"];
arr.unshift("m"."w");
console.log(arr);
Copy the code
Delete the pop shift splice
Requirement: Asks to delete the last piece of array data (delete) with POP
// The pop method of the array removes the last item in the array and returns the deleted item to us
let arr = ["a"."b"."c"];
let res = arr.pop();
console.log(res);
console.log(arr);
Copy the code
Requirement: To delete the first item of the array (delete) with Shift
Shift removes the first piece of data in the array and returns it to us
let arr = ["a"."b"."c"];
let res = arr.shift();
console.log(res);
console.log(arr);
Copy the code
Delete index 1 from array (delete)
// Delete 1 data from the element with index 1
/* Argument 1: Where to start Argument 2: How many elements to delete */
let arr = ["a"."b"."c"];
arr.splice(1.1);
console.log(arr);
Copy the code
Requirement: Delete all data in array except 0 (delete)
let arr = ["a"."b"."c"];
arr.splice(1); // Start from 1 to end
console.log(arr);
Copy the code
To splice
Requirement: Change index 1 to M (change)
let arr = ["a"."b"."c"];
arr[1] = "m";
console.log(arr);
Copy the code
Requirement: change index 1 to D and index 2 to E (change) splice
let arr = ["a"."b"."c"];
arr[1] = "d";
arr[2] = "e";
console.log(arr);
Copy the code
/ / use. Splice ()
/* Parameter 1: where to start Parameter 2: How many elements to replace Parameter 3 Start: new content */
arr.splice(1.2."d"."e");
console.log(arr);
Copy the code
check
Need: get the data with index 1 in the array
let arr = ["a"."b"."c"];
console.log(arr[1]);
Copy the code
Common methods of arrays
toString join concat … Splice Reverse indexOf lastIndexOf includes
Note:
- In some methods the second argument refers to the truncated length such as splice (and the string method substr < this does not change the original array, unlike splice >)
- There are methods where the second argument refers to an array index such as slice (closed left open right) (and the string method substring)
- Don’t guess. Just use it and look it up
-
Let arr = [1, 2, 3, 4, 5];
arr = [] Copy the code
arr.length = 0; Copy the code
arr.splice(0); Copy the code
arr.splice(0, arr.length) Copy the code
-
How do I convert an array to a string
let str = arr.toString(); console.log(str); console.log(typeof str); Copy the code
-
Converts an array to the specified format string
// Join calls toString() by default if no arguments are passed; The // join method, if it passes a parameter, takes that parameter as an element and its join symbol let str = arr.join("+"); console.log(str); console.log(typeof str); Copy the code
-
Concatenate two arrays into an array concat
let arr1 = [1.3.5]; let arr2 = [2.4.6]; // Note: Arrays cannot be concatenated using a plus sign. If they are concatenated using a plus sign, they will be converted to a string before concatenating // let res = arr1 + arr2; / / 1,3,52,4,6 let res = arr1.concat(arr2); console.log(res); // Note: the original array will not be modified. A new array will be generated and returned to us console.log(arr1); console.log(arr2); Copy the code
Extended operator
let arr1 = [1.3.5]; let arr2 = [2.4.6]; /* Note: The extension operator in the destruct assignment (left side of the equals sign) means to pack the rest of the data into a new array. The extension operator on the right side of the equals sign, then, means to unpack all the data in the array and put it where it is */ let res = [...arr1, ...arr2]; // let res = [1, 3, 5, 2, 4, 6]; console.log(res); console.log(typeof res); // Note: the original array will not be modified. A new array will be generated and returned to us console.log(arr1); console.log(arr2); Copy the code
-
Invert the contents of an array
let arr = [1.2.3.4.5]; let res = arr.reverse(); console.log(res); // Note: the original array will be modified console.log(arr); Copy the code
-
Intercepts the contents of the specified range in an array
Splice, which modifies the array, returns the intercepted portion
const months = ['Jan'.'March'.'April'.'June']; months.splice(1.0.'Feb'); // inserts at index 1 console.log(months); // expected output: Array ["Jan", "Feb", "March", "April", "June"] months.splice(4.1.'May'); // replaces 1 element at index 4 console.log(months); // expected output: Array ["Jan", "Feb", "March", "April", "May"] Copy the code
-
Finds the position of an element in an array
let arr = [1.2.3.4.5.3]; /* indexOf: if the specified element is found, it returns the position of the element. If the specified element is not found, it returns -1 let res1 = arr.indexOf(3); console.log(res1); / / 2 let res2 = arr.indexOf(6); console.log(res2); // -1 Copy the code
/* Argument 1: the element to find Argument 2: Where to start the search return value: still the element in the array corresponding position */ let arr = [1.2.3.4.5.3]; let res = arr.indexOf(3.4); console.log(res) Copy the code
/* Note: the lastIndexOf method searches from right to left by default. Once found, it will immediately stop searching for the returned value: the corresponding position of the element in the array */ let arr = [1.2.3.4.5.3]; let res = arr.lastIndexOf(3); console.log(res); Copy the code
-
Determines whether the array contains an element
// Check whether -1 is the result of indexOf or lastIndexOf let arr = [1.2.3.4.5]; // let res = arr.indexOf(8); // -1 let res = arr.lastIndexOf(8); // -1 Copy the code
// An includes method is added to ES6 let arr = [1.2.3.4.5]; let res = arr.includes(4); console.log(res); // false Copy the code
2 d array
-
What is a two-dimensional array? A two-dimensional array is an array where each element of an array is an array, so we call that a two-dimensional array
let arr = [[1.3], [2.4]].Copy the code
-
How do I manipulate a two-dimensional array?
- How do I get from a two-dimensional arrayTo obtainThe data?
- Array name [2d array index]; // Get a one-dimensional array
- Array name [two-dimensional array index][one-dimensional array index]; // Get the elements of the one-dimensional array
- How do I add to a two-dimensional arraystorageThe data?
- Array name [2d array index] = 1d array;
- Array name [2d array index][1d array index] = value;
- How do I get from a two-dimensional arrayTo obtainThe data?
-
Iterate over a two-dimensional array
let arr = [[1.3], [2.4]].for(let i = 0; i < arr.length; i++){ let subArray = arr[i]; // console.log(subArray); for(let j = 0; j < subArray.length; j++){ console.log(subArray[j]); }}Copy the code