Methods (APIs) for all arrays of directories
Array method of ES3
join();
- Purpose: to convert an array to a string
- Whether to change the original array: return a new string without changing the original array
-
let arr = ["a","b","c","d","e"]; let arr1 = arr.join(); console.log(arr1); // a,b,c,d,e console.log(arr); // ["a", "b", "c", "d", "e"] does not change the original array let arr2 = arr.join("-"); console.log(arr2); // a-b-c-d-e
reverse();
- Purpose to reverse the order of the elements in the array
- Change original array: change the original array
-
let arr = ["a","b","c","d","e"]; let arr1 = arr.reverse(); console.log(arr1); // ["e", "d", "c", "b", "a"] console.log(arr); // ["e", "d", "c", "b", "a"] change the original array //How to invert strings? //First convert the string to an array, and then reverse the array let str = "abcde"; let str1 = str.split("").reverse().join(""); console.log(str1); // edcba
sort();
- Purpose: to sort the elements in the array according to certain rules (must be numbers)
- Change original array: change the original array
-
let arr = [5,4,6,1,8,2,9]; let arr1 = arr.sort(); console.log(arr1); // [1, 2, 4, 5, 6, 8, 9] console.log(arr); // [1, 2, 4, 5, 6, 8, 9] change the original array
concat();
- The purpose is to connect two or more arrays
- Whether to change the original array: return a new array without changing the original array
-
let arr1 = [5,4,6,1,8,2,9]; let arr2 = ["a","b","c","d"]; let arr3 = arr1.concat(arr2); console.log(arr3); // [5, 4, 6, 1, 8, 2, 9, "a", "b", "c", "d"]
slice(start,end);
- Purpose: to return the selected element from the existing array
- Whether to change the original array: return a new array without changing the original array
-
let arr = ["a","b","c","d"]; let arr1 = arr.slice(0,1); let arr2 = arr.slice(1) console.log(arr1); // ["a"] console.log(arr2); // ["b", "c", "d"]
Array method of ES5
map();
*Objective: to create a new array. The result is the result returned after each element in the array calls a provided function
- Whether to change the original array: return a new array without changing the original array
-
let list = [1,2,3,]; const data = list.map(item=>(item * 2)); console.log(data); // [2, 4, 6] console.log(list); // The original array remains unchanged
filter();
- Purpose: pass each element of the array to the specified function and return an array containing the return value of the function
- Whether to change the original array: return a new array without changing the original array. The new array is a subset of the original array
-
let arr = [1,2,3,4,5,8,9,10]; let list = arr.filter((x) => { return x>2; }) console.log(list); // [3, 4, 5, 8, 9, 10]
indexOf() and lastIndexOf()
- Purpose: to search for elements with a given value in an array.
- indexOf() refers to query from front to back. lastIndexOf() refers to query from back to front
-
let arr = [2, 4, 6, 8, 8, 6, 4, 2]; console.log(arr.indexOf(4, 2)); //Find 6 starting at index 2 console.log(arr.indexOf(4)); //Start from scratch, find the first one and stop 1 let arr = [2, 4, 6, 8, 8, 6, 4, 2]; console.log(arr.lastIndexOf(4)); //Go forward from the back, but return to the index 6 from the front to the back console.log(arr.indexOf("2")); //Return - 1 not found
Array method of ES6
Array.from();
- Objective: to convert class array objects and traversable objects into arrays
-
let obj = "ffkfkfk"; let list = Array.from(obj); console.log(list); // ["f", "f", "k", "f", "k", "f", "k"]
Array.of()
- Purpose: to convert a set of values into an array
-
let arr = [1,2,3,4]; let list = Array.of(arr); console.log(list); // [Array(4)]
copyWithin();
- Purpose: inside the array, copy the members at the specified location to other locations (the original members will be overwritten)
- Modify original array: modify the original array
-
let fruits = ["Banana", "Orange", "Apple", "Mango"]; let list = fruits.copyWithin(2, 0); console.log(list) // ["Banana", "Orange", "Banana", "Orange"]
find() and findIndex()
- Purpose: find the first qualified array member inside the array
-
Find the element and return the value found. If not found, return undefined. const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] const ret1 = arr1.find((value, index, arr) => { return value > 4 }) const ret2 = arr1.find((value, index, arr) => { return value > 14 }) console.log('%s', ret1) // 5 console.log('%s', ret2) // undefined
fill();
- Purpose: fill an array as you like
-
const fruits = ["Banana", "Orange", "Apple", "Mango"]; const list = fruits.fill("Runoob"); console.log(list); // ["Runoob", "Runoob", "Runoob", "Runoob"]
reduce() method
The reduce() method is used to call the specified callback function for all elements in the array. The return value of the callback function is the cumulative result, and this return value is provided as a parameter when the callback function is called next time
grammar
array.reduce(callback[, value])
parameter
Array: required, an array object
Callback: required. For each element in the array, the reduce method will call the callback function once
Value: optional. If value is specified, it will be used as the initial value to start accumulation. The first call to the callback function takes this value as an argument
Return value
The cumulative result obtained by the last call to the callback function
example
<script> var arr = [1, 2, 3, 4, 5, 6, 7] function getSum(total, num){ return total + num; } var sum = arr.reduce(getSum) console.log(sum); </script>