Whether it’s in everyday coding or in a job interview! Everyone has problems with arrays!

In order to avoid staying up late looking for ways to handle arrays, I conclude this article!

Found some function methods while typing an array in console!

Let’s take a look at the array property manipulation methods.

concat

The concat() method creates a new array by merging (joining) existing arrays.

Syntax: string.concat(string1, string2… , stringX)

parameter describe
string1.string2. .stringX A necessity. One or more string objects to be concatenated into a string.
Var num1 = ["1", "2"]; var num2 = ["3", "4", "5"]; var num = num1.concat(num2); / / [1, 2, 3, 4, 5]Copy the code
Var num1 = ["1", "2"]; var num2 = ["3", "4", "5"]; var num3 = ["6", "7"]; var num = num1.concat(num2,num3); / /,2,3,4,5,6,7 [1]Copy the code

constructor

The constructor property returns a reference to the array function that created this object.

var arr=new Array(); If (arr.constructor==Array){document.write(" this is Array "); } arr.constructor; //function Array() { [native code] }Copy the code

copyWithin

The copyWithin() method is used to copy elements from one specified location in the array to another specified location in the array.

Syntax: array.copywithin (target, start, end)

parameter describe
target A necessity. Copy to the specified target index location.
start Optional. The starting position of the element copy.
end Optional. Index location to stop replication (default isarray.length). If it is negative, it is the reciprocal.

Example:

/ / copy behind the front two elements to the array of two elements on var num = [" 1 ", "2", "3", "4"); num.copyWithin(2, 0); / /,2,1,2 [1]Copy the code

entries

The Entries () method returns an iteration of an array containing the array’s key/value pairs. The index value of the array in the iterated object is the key, and the array element is the value.

Var num = ["a", "b"]; var num = ["a", "b"]; num.entries(); num.entries().next().value; //[0,'a'] num.entries().next().value; //[1,'b']Copy the code

every

The every() method is used to check whether all elements of an array meet the specified criteria (provided by a function).

  • If one element in the array is detected to be unsatisfied, the entire expression is returnedfalseAnd the remaining elements will not be detected.
  • Returns true if all elements satisfy the condition.

Array. every(function(value,key,arr), thisBack)

parameter describe
function(value, key,arr) Must be. Each element in the array executes this function.
value Must be. The value of the current element
key Optional. The index value of the current element
arr Optional. The array object to which the current element belongs
thisBack Optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”. If thisBack is omitted, the value of “this” is” undefined”

Example:

Var nums = [2, 5, 10, 6]; function checkAdult(num) { return num >= 8; } ages.every(checkAdult); //falseCopy the code

fill

The fill() method is used to replace an element of an array with a fixed value.

Syntax: array.fill(value, start, end)

parameter describe
value A necessity. The value of the fill.
start Optional. Start filling the position.
end Optional. Stop filling position (default isarray.length)

Example:

Var nums = ["a", "b", "c", "d"]; nums.fill("e"); //['e','e','e','e']Copy the code
/ / fill "e" to the end of the array of two elements: var fruits = [" a ", "b", "c", "d"]. fruits.fill("e", 2, 4); //["a", "b", "e", "e"]Copy the code

filter

The filter() method creates a new array of elements by checking all eligible elements in the specified array.

Array. filter(function(value,key,arr), thisBack)

parameter describe
function(value, key,arr) Must be. Each element in the array executes this function.
value Must be. The value of the current element
key Optional. The index value of the current element
arr Optional. The array object to which the current element belongs
thisBack Optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”. If thisBack is omitted, the value of “this” is” undefined”

Example:

Var nums = [2, 5, 10, 12]; function checkAdult(num) { return num >= 8; } ages.filter(checkAdult); / / 10, 12Copy the code

find

The find() method returns the value of the first element of the array that passes the test.

The find() method calls the function once for each element in the array:

  • Returns when an element in an array is tested for a conditiontrueFind () returns the element that matches the condition, and the subsequent value is not called.
  • Returns undefined if there are no eligible elements

Array. find(function(value, key, arr),thisBack)

parameter describe
function(value, key,arr) Must be. Each element in the array executes this function.
value Must be. The value of the current element
key Optional. The index value of the current element
arr Optional. The array object to which the current element belongs
thisBack Optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”. If thisBack is omitted, the value of “this” is” undefined”

Example:

// Get the first element in the array greater than 8: var nums = [2, 5, 10, 12]; function checkAdult(num) { return num >= 8; } ages.find(checkAdult); / / 10Copy the code

findIndex

The findIndex() method returns the location of the first element of the array passed in to test the condition.

The findIndex() method calls the function once for each element in the array:

  • Returns when an element in an array is tested for a conditiontrueFindIndex () returns the index position of the element that matches the condition, and the subsequent value is not called.
  • Returns -1 if no element matches the criteria

Array.findindex (function(value, key, arr),thisBack)

parameter describe
function(value, key,arr) Must be. Each element in the array executes this function.
value Must be. The value of the current element
key Optional. The index value of the current element
arr Optional. The array object to which the current element belongs
thisBack Optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”. If thisBack is omitted, the value of “this” is” undefined”

Example:

Var nums = [2, 5, 10, 12]; function checkAdult(num) { return num >= 8; } ages.findIndex(checkAdult); / / 2Copy the code

flat

Flat () transforms a nested array into a one-dimensional array.

Grammar: array. Flat (value)

parameter describe
value Optional. The dimensions of the array to be converted. Infinity means that no matter how many layers you have, you go straight to a one-dimensional array.

Example:

[1,[2, 3]]. Flat (); / / [1, 2, 3] / / specified conversion of nested layer [1, [2, [3, [4, 5]]]]. Flat (2); / / [1, 2, 3, 4, 5]] / / no matter how many layers of nested [1, [2, [3, [4, 5]]]]. Flat (Infinity); // [1, 2, 3, 4, 5]Copy the code

flatMap()

The flatMap() method performs a flat() function on each element of the array after processing it.

Array. flatMap(function(value, key,arr),thisBack)

parameter describe
function(value, key,arr) Must be. Each element in the array executes this function.
value Must be. The value of the current element
key Optional. The index value of the current element
arr Optional. The array object to which the current element belongs
thisBack Optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”. If thisBack is omitted, the value of “this” is” undefined”

Example:

Flat (1, 2, 3). FlatMap (n => [n *2]); flatMap(n => [n *2]); / / (2, 4, 6]Copy the code

forEach

The forEach() method is used to call each element of the array and pass the element to the callback function.

Array. forEach(function(value, key,arr),thisBack)

parameter describe
function(value, key,arr) Must be. Each element in the array executes this function.
value Must be. The value of the current element
key Optional. The index value of the current element
arr Optional. The array object to which the current element belongs
thisBack Optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”. If thisBack is omitted, the value of “this” is” undefined”

Example:

Var arr = [1, 2, 3, 4, 5]; // forEach does not support continue or break. arr.forEach(function (item) { if (item === 3) { return; } console.log(item); }); / / 1,2,4,5Copy the code

includes

The includes() method is used to determine whether a string contains a specified substring. Returns true if a matching string is found, false otherwise.

  • The includes() method is case sensitive.

Syntax: string.includes(value, start)

parameter describe
value Required, the string to find.
start Optionally, set the search to start at that location. Default is 0.

Example:

Var STR = "Hello world"; str.includes("world"); //trueCopy the code

IndexOf, lastIndexOf

The indexOf() method returns the first occurrence of a specified string value in a string.

The lastIndexOf() method returns the last position of a specified string value in the string.

  • Returns -1 if no matching string is found.

Grammar: string. IndexOf (value, start)

Grammar: string. LastIndexOf (value, end)

parameter describe
value A necessity. Specifies the string value to retrieve.
start An optional integer argument. Specifies the starting point in a string for retrieval. Its legal values are 0 to stringobject.length-1. If omitted, the retrieval begins at the last character of the string.
end An optional integer argument. Specifies the last position to retrieve in a string. Its legal values are 0 to stringobject.length-1. If omitted, the retrieval begins at the last character of the string.

Example:

Var STR ="182345678"; str.indexOf("8"); Var STR ="182345678"; str.lastIndexOf("8"); / / 8Copy the code

map

The map() method returns a new array whose elements are the values processed by the call to the original array element.

The map() method processes the elements in their original array order.

Array. map(function(value, key,arr),thisBack)

parameter describe
function(value, key,arr) Must be. Each element in the array executes this function.
value Must be. The value of the current element
key Optional. The index value of the current element
arr Optional. The array object to which the current element belongs
thisBack Optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”. If thisBack is omitted, the value of “this” is” undefined”

Example:

Var nums = [4, 9, 16, 25]; nums.map(Math.sqrt); / / 5-tetrafluorobenzoic [2]Copy the code

pop

The pop() method removes the last element of the array and returns the deleted element.

  • This method changes the length of the array!

Grammar: array. Pop ()

Example:

Var nums = [1, 2,3,4]; nums.pop(); / / [1, 2, 3]Copy the code

push

The push() method adds one or more elements to the end of the array and returns a new length.

  • The new element is added to the end of the array.

Syntax: array.push(item1, item2… , itemX)

parameter describe
item1.item2. .itemX A necessity. The element to add to the array.

Example:

Var nums = ["1", "2", "3", "4"]; nums.push("5"); [1, 2, 3, 4, 5] / / / / add more digits to the last var nums = array (" 1 ", "2", "3", "4"); nums.push("5","6"); / / [6]Copy the code

Reduce, reduceRight

The reduce() method takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and eventually evaluates to a value.

ReduceRight () adds the items in the array forward from the end of the array.

Syntax: array.reduce(function(total, Value, key, ARR), initialValue)

parameter describe
function(total,currentValue, index,arr) A necessity. The function used to execute each array element. Function parameters:
total A necessity.The initial value, or the return value after the calculation.
value A necessity. The current element
key Optional. The index of the current element
arr Optional. The array object to which the current element belongs.
initialValue Optional. The initial value passed to the function

Example:

Var nums = [1, 2, 3, 4]; function countSum(total, num) { return total + num; } nums.reduce(countSum); //10, add nums.reduceRight(countSum) from left to right; // add 10 from right to leftCopy the code

reverse

The reverse() method is used to reverse the order of elements in an array.

Grammar: array. The reverse ()

Example:

Var nums = [1,2,3,4]; nums.reverse(); / /,3,2,1 [4]Copy the code

shift

The shift() method removes the first element from an array and returns the value of the first element.

Grammar: array. The shift ()

Example:

Var nums = [1,2,3,4]; nums.shift(); / / / 2 and 4Copy the code

slice

The slice() method returns selected elements from an existing array.

The slice() method extracts a portion of a string and returns the extracted portion in a new string.

Syntax: array.slice(start, end)

parameter describe
start Optional. Specify where to start. If it is negative, it specifies the position from the end of the array. If this parameter is negative, it extracts from the penultimate element of the original array, and slice(-2) extracts from the penultimate element of the original array to the last element, including the last element.
end Optional. Specify where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the shard array contains all elements from start to the end of the array. If this parameter is negative, it indicates the penultimate element in the original array at the end of the extraction. Slice (-2,-1) extracts the penultimate element of the array to the last element (excluding the last element, i.e. only the penultimate element).

Example:

Var nums = [1,2,3,4]; Nums. Slice (1, 3); Var nums = [1,2,3,4]; nums.slice(-1); / / [4]Copy the code

some

The some() method is used to check whether elements in an array satisfy the specified condition (provided by the function).

The some() method executes each element of the array in turn:

  • If one element satisfies the condition, the expression returnstrueThe remaining elements will not be tested.
  • If no element satisfies the condition, false is returned.

Array. map(function(value, key,arr),thisBack)

Example:

Var nums = [2, 5, 10, 12]; function checkAdult(num) { return num >= 8; } ages.some(checkAdult); //trueCopy the code

sort

The sort() method is used to sort the elements of an array.

  • The sorting order can be alphabetic or numeric and in ascending or descending order.
  • The default sort order is alphabetical ascending.

Grammar: array. Sort (function)

parameter describe
function Optional. Specify the sort order. It has to be a function.

Example:

Var nums = ["b", "c", "a", "d"]; nums.sort(); / / / "a", "b", "c", "d"] / / digital lifting sequence: var nums =,1,3,2 [4]; nums.sort(function(a,b){return b-a}); / /,3,2,1 [4] descending nums. Sort (function (a, b) {return a - b}); / /,3,2,1 [4] ascendingCopy the code

splice

The splice() method is used to add or remove elements from an array.

Grammar: array splice (index, num, item1,… ,itemX)

parameter describe
index A necessity. Specifies where elements are added/removed from. This parameter is the index of the array element to start inserting and/or deleting, and must be a number.
num Optional. Specify how many elements should be deleted. It must be a number, but it can be “0”. If this parameter is not specified, all elements from index to the end of the original array are deleted.
item1. .itemX Optional. The new element to add to the array

Example:

Var nums = [1, 2, 3,4]; Nums. Splice (2, 0, 'a', 'b'); / / [1, 2, 'a', 'b', 3, 4] / / remove the three elements of the array, and add new elements: a, b var nums = [1, 2 and 4]; Nums. Splice (2, 1, 'a', 'b'); / / [1, 2, 'a', 'b', 4]Copy the code

toLocaleString

The toLocaleString() method converts the array to a string separated by a comma.

The toLocaleString() method converts a Date object to a string based on local time and returns the result.

Grammar: array. ToLocaleString ()

Example:

Var nums = [1, 2, 3,4]; nums.toLocaleString(); Var Date =new Date(); var Date =new Date(); date.toLocaleString(); / / "9:24:14 2021/7/21 morning"Copy the code

toString

The toString() method can convert arrays to numbered strings. If it is a number, it is converted to a string of several digits.

Grammar: number. ToString (radix)

parameter describe
radix Optional. Specifies the cardinality of a number. It is an integer between 2 and 36. If this parameter is omitted, the cardinality 10 is used. Note, however, that if the parameter is a value other than 10, the ECMAScript standard allows the implementation to return any value. 2-digits are displayed as binary values, 8-digits as octal values, and 16-digits as hexadecimal values

Example:

Var nums = [1,2,3] nums.tostring (); Var num = 15; var num = 15; num.toString(); //15 num.toString(2); //1111 num.toString(8); //17 num.toString(16); //fCopy the code

unshift

The unshift() method adds one or more elements to the beginning of the array and returns the new length.

Syntax: array.unshift(item1,item2… , itemX)

parameter describe
item1.item2. .itemX Optional. Adds one or more elements to the start of the array.

Example:

Var nums = [1,2,3,4]; nums.unshift("a","b"); / / / 'a', 'b', 1, 2, 3, 4]Copy the code

values

The values() method returns a new Array Iterator containing the value of each index of the Array.

Syntax: array.values ()

Example:

const nums = ['a', 'b', 'c']; const iterator = nums.values(); for (const value of iterator) { console.log(value); //a b c }Copy the code

conclusion

The remaining Symbol is the new data type of ES6, __proto__ for details please see another article prototype and prototype chain, do you know?

That’s the end of it. Have you learned? Learn to buckle 1, learn not to buckle brain seeds!