JavaScript you don’t know some array methods

Concat

VAR A = [1, 2, 3]; A.Concat ([4, 5, 6], 7, 8); // [1, 2, 3, 4, 5, 6, 7, 8]
   
Note that the A array does not change, just returns a new Array.

Copywithin

It accepts three parameters. Target (required): Replace data from this location. START: Start reading data from this location, default is 0. If it is a negative value, it represents the countdown.

End (optional): Stop reading data before this location, is equal to the length of the array by default. If it is a negative value, it represents the countdown.

These three parameters should be a value, if not, it will automatically turn to a value



// copy the line No. 3 to the 0 position [ 1, 2, 3, 4, 5] .copywithin (0, 3, 4) /// -2 equivalent to the line No. 3, -1 equivalent to the 4th position [ 1, 2, 3, 4, 5] .copywithin (0, -2, -1) // [4, 2, 3, 4, 5] // Copy the 3 position to the 0 position [] .copywithin. Call ({Length: 5, 3: 1}, 0, 3) // {0: 1, 3: 1, Length: 5} // The 2nd position is ended, copied to the 0 position VAR i32a = new INT32ARRAY ([1, 2, 3, 4, 5]); i32a.copywithin (0, 2); // Int32aRRAY [3, 4, 5, 4, 5] // No deployment TypedarraY ‘s platform // Need to use the following way [] .copywithin.call (New Int32Array ([1, 2, 3, 4, 5]), 0, 3, 4); // Int32Array [4, 2 , 3, 43]

Entries
  VAR A = [1, 2,3]; var en = a.entries (); en.next (). Value; // [0.1];  

Returns an iterative object Every

 Function isbigenough (Element, index, array) {return (element> = 10);} var passed = [12, 5, 8, 130, 44] .every (isbigenough); // passed is falseparassed = [12, 54, 18, 130, 44] .every (isbigenough); // passed is true   
Each item returns True through the test function, otherwise returns false

[1, 2, 3] .fill (4) // [4, 4, 4] [1, 2, 3] .fill (4, 1) // [1, 4, 4] [1, 2, 3] .fill (4, 1, 2) // [1, 4, 3] [1, 2, 3] .fill (4, 1, 1) / / [1, 2, 3]. Fill (4, -3, -2) // [4, 2, 3] [1, 2, 3] .fill (4, nan, nan) // [1, 2, 3] Array (3) .fill (4); // [4, 4, 4] []. Fill.call ({Length: 3}, 4) // {0: 4, 1: 4, 2: 4, Length: 3}
 Changed the array itself   
FILTER

Function isbigenough (value) {return value> = 10;} var filter = [12, 5, 8, 130, 44] .filter (isbigenough); // filtered is [12, 130, 44]

Returns a new array

FIND
   Method Returns the value of the first element of the test function to which the array is met. Otherwise returning undefind 

function isbigenough (Element) {Return Element> = 15;} [12, 5, 8, 130, 44] .find (IsbiGenough) ; // 130

FindIndex

FindIndex () method returns the first element of the test function to which the array is met. index. Otherwise returns -1.
   Function isbiGenough (Element) {Return Element> = 15;} [12, 5, 8, 130, 44] .findIndex (isbigenough); // 3 

Let a = [‘A’, ‘B’, ‘c’]; A.Foreach (Function) {Console. LOG (Element);}); // a // b // C // Syntax Array.Foreach (Callback (CURLENTVALUE, INDEX, Array) {// do something}, this) array.foreach (callback [, thisarg]


Callback
  is a function performed in an array, which receives three parameters: CurrentValue (Current Value) The current element being processed in the array. The index of the current element being processed in the index array. ArrayForeach () method is operating an array. THISARG optional parameters. When performing a callback function (reference object)  

Note: There is no way to stop or jump out of the Foreach loop, except for an exception. If you need this, use the foreach () method is wrong, you can use a simple loop as an alternative. If you are testing whether the elements in an array meet a certain condition, you need to return a Boolean value, you can use Array.every, Array.Some. If available, new method find () or findindEX () can also be used for early termination of true value testing.

Arr.inCludes (Searchelelement) arr.includes (search) arr.includes (search) arr.includes (search) arr.includes (search) arr.includes (search) arr.includes (search) arr.includes (search) arr.includes (search) arr.includes ]
  Searchelement  
The element value is required. fromNDEX

Optional starts searching for Searchelement

from this index. If you are negative, you start searching from Array.Length + fromNDEX index. Default is 0. Return value
  A boolean.  
[1, 2, 3] .includes (2); // True [1, 2, 3] .includes (4); // False [1, 2, 3] .includes (3, 3); // False [1, 2, 3] .includes (3, -1); // True [1, 2, nan] .includes (nan); // true

IndexOf
   Arr.indexof (Searchelelement) arr.indexof (Searchelement [, FromIndex = 0]) 
Parameter

Searchelement

The element to be found

fromNDEX
Start the location of the lookup. If the index value is greater than or equal to the array length, it means that it will not look for in an array, return -1. If the index value provided in the parameter is a negative value, it is a cancellation of the end of the array, ie -1 indicates that from the last element starts searching, and the second element starts looking for the second element, in this type. Note: If the index value provided in the parameter is a negative value, an array is still queried from the previously. If the index value after the cancellation is still less than 0, the entire array will be queried. Its default is 0.
  Return value  
The first index position found in the array; if it is not found, it returns -1

Let a = [2, 9, 7, 8, 9]; a.indexof (2); // 0 a.indexof (6); // -1a.indexof (7); // 2a .indexof (8); // 3a.indexof (9); // 1IF (A.indexof (3) === -1) {// Element Doesn’t exist in array}

JOIN

Str = arr.join () // default “,” str = arr.join (” “) // separator === Empty string” “Str = arr.join (separator) // separator

Keys

Keys () method Returns a new Array iterator that contains keys
   

Let Arr = [“A”, “B “,” c “]; let itness= Arr.keys (); // UndefinedConsole.log (item); // array itrator {} console.log (item ()); // Object {value: 0, Done: false} console.log (Iterator .next ()); // Object {value: 1, done: false} console.log (iterator.next ()); // Object {value: 2, DONE: FALSE} console.log (item.next () ); // Object {value: undefined, done: true}

MAP
map () method creates a new array The result is that each element in the array calls a result that is returned after the provided function.
   
Let Array = Arr.map (Function Callback (CurrentValue, Index, Array) {// Return Element for new_array} [, thisarg]) let NumBers = [1, 5, 10, 15]; Let Doubles = Numbers.map ((x) => {Return x * 2;}); // Doubles IS now [2, 10, 20, 30] // NumBers is Still [1, 5, 10, 15] let NumBers = [1, 4, 9]; let roots = numbers.map (math.sqrt); // rOOTS IS NOW [1, 2, 3] // NumBers Is Still [1, 4, 9]
Callback

Generates a function of new array elements using three parameters:

currentValue

of the first parameter, callback, the array element currently being processed.

INDEX

The second parameter of Callback, the index of the current element being processed in the array.

array

The third parameter callback array, map the called method.

THISARG
Optional. The THIS value used when performing the Callback function.
  Return value  
A new array, each element is the result of the callback function.
POP ​​and PUSH

The POP () method removes the last element from the array and returns the value of the element. This method changes the length of the array.

Push () method adds one or more elements to the end of the array and returns the new length of the array.
Arr.push (Element1, …, Elementn)
 The two arrays   This example adds all elements of the second array using Apply (). 
Note that when the second array (moreVEGS in the example) is too large, this method is not used to merge an array, because the number of parameters that acts can be accepted is limited. Specific reference Apply ()

var vegetables = [‘PARSnip’, ‘Potato’]; var morevegs = [‘CELERY ‘,’ Beetroot ‘]; // Mark the second array into the first array // is equivalent to Vegetables.push (‘ Celery ‘,’ Beetroot ‘); Array.Prototype.push.Apply (VGetables, morevegs) Console.log (vegetables); // [‘Parsnip’, ‘Potato’, ‘Celery’, ‘Beetroot’]

 rCE and ReduceryT   
The method () method applies a function to each element (from left to right) in the accumulator and array, which reduces it as a single value.

Array.Reduce (Function (Accumulator, CurrentValue, INITIALVALUE) VAR TOTAL = [0, 1, 2, 3] .REDUCE (Function) (SUM , value) {Return Sum + Value;}, 0); // Total is 6Var flattened =[[0, 1], [2, 3], [4, 5]]. Reduce (Function (A, B) { Return a.concat (b);}, []); // Flattened IS [0, 1, 2, 3, 4, 5]

Callback
  Perform a function of each value in the array, contains four parameters  
Accumulator
to call the callback returned by the callback, or the initial value provided ( InitialValUE)

The elements in the array are being processed

CurrentIndex

The element is being processed, if INITIALVALUE is provided, starting from 0 Otherwise, from 1

Array

calls the array of reduu

InitialValue

Options, the value is used for the first time called Callback’s first Parameters. If the initial value is not set, the first element in the array is used as the initial value. When the empty array calls Reduce, no initial value will be reported.

PS: Contrary to the execution direction of ketuceright () and reduce ()

Reverse

REVERSE method to reverse the position of the element in the array and returns a reference to the array.

SHIFT and UNSHIFT

SHIFT () method removes the first element from the array and returns the value of the element. This method changes the length of the array.

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

Slice () method Returns a part of a part of the array selected from the beginning to the end (excluding end) to a new array object. The original array will not be modified.

Arr.slice (); // [0, end]; arr.slice (start); // [start, end]; arr.slice (Start) (START, END); // [start, end];

SLICE does not modify the original array, only a new array of elements in the original number group will only be returned. Original arrayIf the following rules are copied:

If the element is an object reference (not an actual object), the slice will copy this object to a new array. Both object references reference the same object. If the referenced object changes, the elements in the new and original arrays will also change.

For strings, numbers, and boolean values ​​are not String, Number or Boolean, SLICE will copy these values ​​to new arrays. Modify these strings or numbers or Booleans in other arrays, will not affect another array.

If new elements are added to the two numbers, the other will not be affected
  Some  
Some () method test Some elements in the array are tested by the provided function.

const isbiggerthan10 = (Element, index, array) => {return element> 10;} [2, 5, 8, 1, 4] .some (isbiggerthan10) // false [12, 5, 8, 1, 4] .some (isbiggerthan10); // true

Tolocalestring and Tostring
 TOLOCALESTRING () Returns a string representation of the elements in the array. The elements in the array will use their respective Tolocalestring methods to string, which will use a string of a particular locale (such as a comma ",").   
VAR Number = 1337; var Date = new date (); var myarr = [Number, Date,”foo”]; var str = myarr.tolocalestring (); console.log (str); // output “1,337,2017/8/13 8: 32: 24, foo” // assume that running in Chinese (zh- CN) Environment, Beijing Time Zone VAR A = 1234a.toString () // “1234” A.TOLOCALESTRING () // “1,234” // When the number is four or more, Tolocalestring () will make the three three digits One separation, like us sometimes numbers will also three a semicolol var sd = new date () SD // WED FEB 15 2017 11:21:31 GMT + 0800 (CST) sd.tolocalestring () // “2017 / 2/15 11:21:31 “SD.TOSTRING () //” WED FEB 15 2017 11:21:31 GMT + 0800 (CST) ”

SPLICE

splice () method changes the content of an array by deleting existing elements and / or adding new elements.

Array.splice (start) array.splice (start, deletecount) array.splice (Start, Deletecount, item1, item2, …)

START

Specifies the modified start position (from 0 count). If the length of the array is exceeded, the content is started from the end of the array; if it is a negative value, the first few bits (from 1 count) starting from the end of the array.

DELeTECount is optional

integer, indicating the number of array elements to be removed. If deletecount is 0, it is not removed. In this case, at least one new element should be added. If deletecount is greater than the total number of elements after start, it will be deleted from the elements behind Start (including the START bit).

If deletecount is omitted, it is equivalent to (arr.length – start).

Item1, Item2, … Optional

To add an element of an array, start from the START position. If not specified, splice () will only delete an array element.

Return value

An array consisting of deleted elements. If only one element is deleted, the array containing only one element is returned. If you do not delete an element, return an array.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment