Common methods of Array array Math method

Math method

The Math.abs() function returns the absolute value of a number.
The Math.min() method returns the minimum value in a specified set of data.
The Math.max() method returns the maximum value in the specified data.
Math.ceil() function, round up
Math.floor() method, round down
Math.round(), returns a number rounded integer.
The Math.random() function returns a floating point, pseudo-random number ranging from 0 to less than 1, excluding 1 from 0 up.
The Math.trunc() function returns the integer part of a number, regardless of whether it is positive or negative, directly remove the decimal point and the following part.
The Math.sqrt() method returns the square root of a number.
The Math.pow() method returns the base (base) to the power of the exponent (exponent).
The Math.cos() function returns the cosine of a value.
The Math.trunc() function returns the integer part of a number, regardless of whether it is positive or negative, directly remove the decimal point and the following part.

Common methods of Array arrays

Array.from(), converts an array-like object to a real array

let arr = [12,45,97,9797,564,134,45642]
let set = new Set(arr)
console.log(Array.from(set))  // [ 12, 45, 97, 9797, 564, 134, 45642 ]

push() tail append returns the length of the modified array.
unshift() header addition returns the length of the modified array.
pop() tail removal returns the removed item.
shift() head removal returns the removed item.

fill() fills the array (the original array changes)
copyWithin overwrites within the array (the original array changes)
concat: Add parameters to the original array. This method first creates a copy of the current array, then adds the received parameters to the end of the copy, and finally returns the newly constructed array. When no parameters are passed to the concat method, it simply copies the current array and returns a copy.
reverse (the original array is changed) reverse
sort (the original array is changed) sort
join array to string
split string to array
indexOf: Returns the first index where a given element can be found in the array, or returns if it does not exist -1.
lastIndexOf: The method returns the specified element, the index of the last one in the array, or -1 if it does not exist. (Look forward from the back of the array)
ES7 includes() Definition: Returns a Boolean value indicating whether an array contains a given value

There are a total of 12 methods for traversing
an array in js without changing the original array:

ES5
forEach、every 、some、 filter、map、reduce、reduceRight
ES6
find、findIndex、keys、values、entries

forEach
map
filter
some
every
reduce
find() definition: returns the first qualified array member, if there is no qualified member, returns undefined.
findIndex() Definition: Returns the index of the first array member that meets the criteria, or -1 if all members do not meet the criteria.
flat() Array flattening, recursively traverse the array according to a specified depth, and combine all elements and elements in the traversed sub-array into a new array and return.

keys()&values()&entries()
keys to traverse key names, values ​​to traverse key values, entries to traverse key names + key values
​​Definition: All three methods return a new Array Iterator object, which contains different values ​​according to different methods.

for (let index of ['a', 'b'].keys()) {
    
    
  console.log(index);
}
// 0
// 1

for (let elem of ['a', 'b'].values()) {
    
    
  console.log(elem);
}
// 'a'
// 'b'

for (let [index, elem] of ['a', 'b'].entries()) {
    
    
  console.log(index, elem);
}
// 0 "a"
// 1 "b"

Guess you like

Origin blog.csdn.net/qq_43940789/article/details/131293686