Common methods and skills in JS array learn to advance and become a big boss

  • 2021-11-29 05:57:26
  • OfStack

Catalog splice () method join () method reverse () method every () method reduce () method filter () method findIndex () method and find () method findIndex () find () forEach () method some () method indexOf () method sort () method push () method pop () method unshift () method shift () method

splice () method

Intercept and replace array

The first parameter is the starting position, the second is the number of truncations, the third is the replacement element, the return value is the truncated element, and the original array is the remaining elements.

join () method

Array to string

(), if you enter an empty string join (''), you can splice directly without splicing

For example:


let aaa = [1,2,3]
let bbb = aaa.join('-')  
let ccc = aaa.join('0')  
console.log(bbb) // '1-2-3'
console.log(ccc) //'10203'

reverse () method

Flip an array

arr. reverse () flips the array, and the return value is the flipped array

every () method

Find an array that does not meet the requirements

false is returned if one item does not meet the conditions defined by you, and true is returned only if every item meets the conditions

The first parameter is every entry of the array, the second is the index, and the third is the current array

Example:


arr:[121,3322,3215,3549,6899,46456]
arr.every(function(item,index,array){
    return item>2000  // Detects each of the arrays 1 Is the value greater than 2000
})	// The result is false  Unless each value of the array is greater than 2000  Is true

reduce () method

Find the accumulated value

You can calculate the results of previous array item traversal with the current traversal item

The first parameter is the accumulator (holding the result returned by the second value) prev

The second value is the value currently being processed (traversing the array from beginning to end) cur

The third index index

The 4th current array arr

The fifth initial value (after the function) init

Example:


var arr = [3,9,4,3,6,0,9];
var sum = arr.reduce(function (prev, cur) {
    return prev + cur;
},0);// Because the initial value is 0  That is prev The initial value is 0   The cable is calculated as it is   0+3=3  3+9=12  12+4=16 ....  Every time the result is obtained, it will be prev Under storage progress 1 Secondary operation   This is the simplest reduce Summation 

filter () method

Traverse and filter arrays

The first parameter is every entry of the array, the second is the index, and the third is the current array

It will traverse the array, filter the conditions you defined, and return a new array containing all the elements that meet the conditions

Example:


var arr=[1,3,5,7,8]
var sum = arr.filter(function(value,index,arr){
    return value >3  // Screening arr In the value   Greater than 3 Elements of  
})
console.log(sum) // The value returned is [5,7,8]

findIndex () method and find () method

findIndex()

Find the index of the first array member that meets the condition. If the index cannot be found, return-1

For empty arrays, the function is not executed and does not change the original value of the array.

find()

The find () function is used to find the target element. If it is found, it returns the element. If it is not found, it returns undefined

The lookup function takes three arguments:

value: Array elements found every 1 iteration.

index: Index of array elements found every 1 iteration.

arr: Array looked up.

forEach () method

Traversing circular arrays
The first value is each parameter
The second value is the index
The third is the array itself
Used to traverse the elements in an array


arr:[1,2,3]
arr.forEach(function(item,index,array){
console.log(item) //1,2,3
})

some () method

Detects whether the elements in the array meet the condition, which is used to find that the only value of 1 returns true or false


var a = arr.some(function(item,index,array){
return item>3  // Detect whether there is a greater than 3 If there is, return the element of true If there is no, return false
})

Terminate the loop as long as one element satisfies the condition is found. If return is encountered in some, trun will terminate the loop

indexOf () method

Finds whether an element exists in the array and returns the subscript. Returns the first index in the array where a given element can be found, or-1 if it does not exist.
Parameter
Parameter 1 (required): Element to be found
Number 2 (optional): The location to start the lookup (not greater than or equal to the length of the array, returns-1), accepts a negative value, and the default value is 0.
Strictly equal search:
The indexOf search for arrays is different from the indexOf search for strings. The indexOf search for arrays uses strict equality = = = to search for elements, that is, the elements of arrays must match exactly to search successfully.

sort () method

Optional parameter: A comparison function that specifies the sort order.
By default, if the sort () method does not pass the comparison function, it is in ascending alphabetical order by default. If the element is not a string, the toString () method will be called to convert the element into the Unicode (universal code) locus of the string, and then compare the characters.


//  String arrangement   Initials 
var a = ["Banana", "Orange", "Apple", "Mango"];
a.sort(); // ["Apple","Banana","Mango","Orange"]
//  When numbers are sorted,   Because converting to Unicode After the string, some numbers will be compared and ranked behind 
var a = [10, 1, 3, 20,25,8];
console.log(a.sort()) // [1,10,20,25,3,8];

push () method

push Adds new elements to the end of an array (you can add more than one at a time)
Return value: length (length) of the new array


const aa = [1,2,3]
aa.push(5,6)
console.log(aa) // [1,2,3,5,6]

pop () method

Delete 1 return value at the end is the deleted element

unshift () method

Header is added, and the return value is array length

shift () method

Header deleted element return value: deleted element

The above is the JS array commonly used methods and skills learned to become a big boss in detail, more information about JS array commonly used methods and skills please pay attention to other related articles on this site!


Related articles: