Create an array
- Let arr = [1, 2, 3]
- Let arr = new Array(1,2,3)
Ooj letarr = [1,2.3] letarr = newArray (1,2,3)
Create arrays with strings
- Let STR = ‘1,2,3’ str.split(‘,’)
- Let str2 = ‘1,2,3’ str2.split (”)
- Array.from(‘1,2,3’) turns something that isn’t an Array into an Array,
You have to have 0,1,2,3, and you have to have the length attribute. For example: Array. The from ({0: ‘a’, 1: ‘b’, 2: ‘c’, 3: ‘d’, length: 4}) if the length is 2, wrong is that (” a “, “b”)
Pseudo array
Let divList = document.querySelector(‘div’);
Merge array
- Let arr3 = filling [3]
- Let arr4 =,4,4,4 [4]
- Arr3. Concat (arr4) results in [3,3,3,4,4,4,4]
Interception of array
- Let arr5 =,2,3,4,5,6,7,8,9 [1]
- Arr5. Slice (2) results in [3,4,5,6,7,8,9]
Arr1.slice (1)// Start with the second element arr1.slice(0)// all intercepts
Add and delete
- Delete the header element
Arr.shift ()//arr is modified and returns the deleted element
- Delete the trailing element
Arr.pop ()//arr is modified and returns the deleted element
- Delete the middle element
Arr.splice (index,1)// Delete an element of index (index,1). Arr is number) after the splice (index, 1, ‘x’) / / add and delete position ‘x’ arr. Splice (index, 1, ‘x’, ‘y’) / / and add ‘x’ in the delete position, ‘y’
View all elements
- Let arr = [1, 2, 3, 4, 5];
- arr.x = ‘x’
- Object.keys(arr)
- Conclusion: [” 0 “, “1”, “2”, “3”, “4”, “x”]
- The result above is the property name
- Object. Values (arr)
- [1,2,3,4,5, ‘x’]
- The result above is the property value
For (let I = 0; i < arr.length; I ++){consle.log(I)}
- 0
- 1
- 2
- 3
- 4
**for (let i = 0; i< arr.length; I++) {consle. Log (‘ I: {I} : I: {arr [I]} ‘)} * * method 2: Arr.foreach (function(item,index){console.log(‘ index:{index}:index:{item}’)}) equals’
- `
function forEach(array,fn){
for(let i=0; i<array.length; i++){ fn(array[i],i,array) } } forEach(['a'.'b'.'c'].function(x,y,z){consoleLog (x, y, z)})VM900:1 a 0 (3) ["a"."b"."c"]
VM900:1 b 1 (3) ["a"."b"."c"]
VM900:1 c 2 (3) ["a"."b"."c"] # #forWhat's the difference between a loop and a forEachforIn the loopcontinueandbreak.forLoop is the keyword, forEach is the normal function;forIs a block element, forEach is a function;` ``js for(let i = 0; i< arr.length; i++){ console.log(`${i} : ${arr[i]}') if (I ===3) {break; }} The 0:1 1:2 2:3 3:4 for loop can terminate or continue from any position, but forEach can only end to endCopy the code
Viewing individual properties
Letarr = [111,222,333] arr [0] yields 111
The index of crossing the line
“Cannotreadproperty ‘toString’ ofundefined” is crossed
Find if an element is in an array
arr
Increments an element in an array
Arr. push(item1, Item2)// Modify arr to return the new length
let arr = [1.2.3.4.5.6.7.8]
arr.push(9) or arr. Push (a, b, c)` `Add element arr. Unshift (newItem) to the header// Modify arr to return the new length
arr.unshift(item1.item2)// Modify arr to return the new lengthAdd arr. Splice (index,0.'x')// Insert 'x' at index
arr.splice(index,0.'x'.'y') ### reverse the order arr.reverse()` `'js reverses array let arr = [1,2,3,4,5,6,7,8] arr.reverse() returns [8,7,6,5,4,3,2,1] reverses string var s = 'abcde' s.split('' Draw [" a ", "b", "c", "d", "e"] s.s plit (' '). The reverse () it is concluded that [" e ", "d", "c", "b", "a"] s.s plit (' '). Reverse () join (' ') / / an array into a string It is concluded that "edcba"Copy the code
Out of order how to sort
Note: we just need to decide which one is big and which one is small, and leave sorting to the function.
let arr = [5.2.4.3.1] method 1: arr.sort() [1.2.3.4.5[法二 : arr.sort()function(a,b){
if(a>b){
return 1
}else if(a===b){
return 0}
else{
return -1}}) results [1.2.3.4.5Arr. Sort ()function(a,b){
if(a>b){
return -1
}else if(a===b){
return 0}
else{
return 1}}) results [5.4.3.2.1]
Copy the code
Take another example of a complex array
let arr = [
{name:'Ming'The score:99}, {name:'little red'The score:95}, {name:'rhubarb'The score:100}]
arr.sort(function(a,b){
if(a.score>b.score){return 1}
else if(a.score===b.score){return 0}
else{return -1}}) results in (3) [{...}, {...}, {...}]0: {name: "Little red".score: 95}
1: {name: "Xiao Ming".score: 99}
2: {name: "Rhubarb".score: 100}
length: 3You can also write arr.sort((a,b) = > a.score-b.score)
Copy the code