A static method
-
Array.isarray (): checks whether it is an Array
-
Array.from(): Converts a pseudo-array to a true Array
Note: The pseudo-array must have the length attribute, otherwise it cannot be converted to the true array. How many values does the array have? If the length is greater than the original number of data, the extra part is undefined
Instance methods
1. Concat: Link arrays become new arrays
let a1 = [1.2.3];
let a2 = ['a'.'b'.'c'];
let a3 = [11.12.13];
let arr = a1.concat( a2, a3, 'red');
// Return the new array after concatenation;
Copy the code
(Find an element in an array and return the index if it is found, or -1 if it is not found, and locate the element where it first appeared.)
let arr = ['a'.'b'.'c'.'d'.'a'.'c'];
arr.indexOf('c'); // The returned index value is2 ;
Copy the code
(Find the last position of an element in the array and return the index if it was found, or -1 if it was not found)
let arr = ['a'.'b'.'c'.'d'.'a'.'c'];
arr.indexOf('c');
// Returns an index of 5;
Copy the code
4. Join: Used to join each element of an array into a string
let arr = ['a'.'b'.'c'.'d'.'e'.'f'];
arr.join(The '-');
1.After joining together for'a-b-c-d-e-f'String;2.If you want to concatenate the contents of the array, simply arr.join(' '); Can; The results for'abcdef'String;Copy the code
5. Sort: array sort (encoding sort, first letter only)
let arr = [1.23.6.13.199.28.96]; Formula: (sort from smallest to largest) arr.sort(function (a,b){returna-b }); (sorted from largest to smallest) arr.sort(function (a,b){return b-a });
Copy the code
The following methods share a sample array
let arr = [1.2.3.4.5.6.7.8.9.10]; // Sample array
Copy the code
6. Find: Returns the first element in the array that meets the criteria
let refind = arr.find(i= > {
return i > 5;
// Default traversal number group, can write condition, directly return the result that meets the condition;
});
console.log(refind);
// The console result is: 6;
Copy the code
FindIndex: Returns the index of the first element in the array that meets the condition
let refindIndex = arr.findIndex(i= > {
return i > 5;
// Default traversal number group, can write condition, directly return the index value of the element that meets the condition;
});
console.log(refindIndex);
// The console result is: 5;
Copy the code
8. Some: Returns true if one of the elements satisfies the condition, otherwise false
let resome = arr.some(i= > {
return i > 5;
// Default traversal number group, can write condition, return Boolean value;* * or * *Copy the code
9. Every: returns true on all elements, otherwise false
let reevery = arr.every(i= > {
return i > 5;
// Default traversal number group, can write condition, return Boolean value;
});
console.log(reevery);
Copy the code
10. Filter: Iterates through the filter elements, filters out the elements that meet the conditions and returns them in a new array
let refilter = arr.filter(i= > {
return i > 5;
// Default traversal number group, can write conditions, return a new array;
});
console.log(refilter);
// Console result is:
(5) [6.7.8.9.10]
0: 6
1: 7
2: 8
3: 9
4: 10
length: 5
__proto__: Array(0)
Copy the code
11. Map: Iterates over elements, executes a callback for each item, and returns the result in a new array
let remap = arr.map(i= > {
return i * i;
// By default, you can write conditions to return the new array after calculation;
});
console.log(remap);
// Console result is:
(10) [1.4.9.16.25.36.49.64.81.100]
0: 1
1: 4
2: 9
3: 16
4: 25
5: 36
6: 49
7: 64
8: 81
9: 100
length: 10
__proto__: Array(0)
Copy the code
12. Reduce: Iterate over elements to obtain the sum of all elements
Syntax: reduce ((The sum, the current term) = >{execute code}, initial value)let theSum = arr.reduce((sum, i) = > {
return sum += i
}, 0)
console.log(theSum)
// The console output is 55
Copy the code
13. Includes: Determines whether an array contains a specified value. Returns true if it does, false otherwise
arr.includes(2) / / return true
arr.includes(11) / / returns false
Copy the code
14. Shift: Removes the first element from an array and returns the value of the first element
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.shift() + "<br />")
document.write(arr)
</script>
/ / output:
// George,John,Thomas
// George
// John,Thomas
Copy the code