Learn the common methods and techniques in JS arrays and become a master

Learn the common methods and techniques in JS arrays and become a master

splice() Method

Intercept and replace array

The first parameter is the starting position, the second is the number of interceptions, and the third is the element to be replaced. The return value is the intercepted element, and the original array is the remaining elements.

join() Method

Array to string

What is the string in () used to connect? If you enter an empty string, join('') will directly concatenate without splitting.

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

Flipping an Array

arr.reverse() reverses the array, and the return value is the reversed array

every() Method

Find arrays that do not meet the criteria

Iterate through each item in the test array. If one item does not meet the conditions you defined, it returns false. The rest will not be executed. Only when every item meets the conditions will it return true.

The first parameter is each item in 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 //Check whether each value of the array is greater than 2000
}) //The result is false unless each value in the array is greater than 2000.

reduce() Method

Find the accumulated value

The result of the previous array item traversal can be calculated with the current traversal item

The first parameter is the accumulator (to store 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

The fourth 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); //Since the initial value is 0, that is, the value of prev at the beginning is 0, the calculation is 0+3=3 3+9=12 12+4=16 .... Each result will be stored in prev for the next calculation. This is the simplest reduce sum

filter() Method

Traversing and filtering an array

The first parameter is each item in the array, the second is the index and the third is the current array.

It will traverse the array and 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 //Filter the elements in arr that are greater than 3})
console.log(sum) //The returned value is [5,7,8]

findIndex() Method and find() Method

findIndex()

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

For an empty array, the function is not executed and the original value of the array is not changed.

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 find function takes three parameters:

value: The array element to search for in each iteration.

index: The index of the array element to be searched in each iteration.

arr: The array to be searched.

forEach() Method

The first value of the loop array is each parameter, the second value is the index, and the third is the array itself. It is mostly used to traverse the elements in the array.

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

some() Method

Checks whether the elements in the array meet the conditions, used to find the unique value and returns true or false

var a = arr.some(function(item,index,array){
return item>3 // Check if there is an element greater than 3, if yes, return true, otherwise return false
})

As long as an element that meets the condition is found, the loop will be terminated. If return trun is encountered in some, the loop will be terminated.

indexOf() Method

Check if a certain element exists in an array and return the index. Returns the first index in the array where a given element can be found, or -1 if it is not present.
Parameters The first parameter (required): the element to be searched The second (optional): the position to start searching (cannot be greater than or equal to the length of the array, returns -1), accepts negative values, the default value is 0.
Strict equality search:
The indexOf search of an array is different from the indexOf search of a string. The indexOf search of an array uses strict equality === to search for elements, that is, the array elements must match exactly for the search to be successful.

sort() method

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

// Arrange the first letters of the string var a = ["Banana", "Orange", "Apple", "Mango"];
a.sort(); // ["Apple","Banana","Mango","Orange"]
// When sorting numbers, some numbers will be larger and ranked at the end after being converted into Unicode strings 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 the array (you can add multiple elements at once)
Return value: 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 the end of the element and the return value is the deleted element

unshift() method

Header added, the return value is the length of the array

shift() method

Head delete element return value: deleted element

The above is the detailed content of how to learn and advance to become a master in the commonly used methods and techniques in JS arrays. For more information about commonly used methods and techniques in JS arrays, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of the new array methods in JavaScript es6
  • Detailed explanation of JS array methods
  • Detailed explanation of common methods of JavaScript arrays
  • Summary of several common methods of JavaScript arrays
  • 12 Useful Array Tricks in JavaScript

<<:  Key points for writing content of HTML web page META tags

>>:  About the configuration problem of MyBatis connecting to MySql8.0 version

Recommend

How to understand JavaScript modularity

Table of contents 1. Browser support 2. export ex...

Summary of the benefits of deploying MySQL delayed slaves

Preface The master-slave replication relationship...

Nginx merges request connections and speeds up website access examples

Preface As one of the best web servers in the wor...

How to use boost.python to call c++ dynamic library in linux

Preface Recently I started using robot framework ...

MySQL database terminal - common operation command codes

Table of contents 1. Add users 2. Change the user...

A brief discussion on the use of Web Storage API

Table of contents 1. Browser local storage techno...

Vue folding display multi-line text component implementation code

Folding display multi-line text component Fold an...

Detailed explanation of common usage of pseudo-classes before and after in CSS3

The before/after pseudo-class is equivalent to in...

A brief discussion on the pitfalls of react useEffect closure

Problem code Look at a closure problem code cause...

jQuery simulates picker to achieve sliding selection effect

This article shares the specific code of jQuery t...

Implementation of drawing audio waveform with wavesurfer.js

1. View the renderings Select forward: Select bac...

Mysql NULL caused the pit

Using NULL in comparison operators mysql> sele...

In-depth understanding of Vue's method of generating QR codes using vue-qr

Table of contents npm download step (1) Import (2...

CentOS8 network card configuration file

1. Introduction CentOS8 system update, the new ve...