JS in the middle of the number of common methods summary (recommended)

It is also almost two years in front of the front end. It usually collects some notes in the impression note, but after collecting, I haven’t seen it.佬 Good points, truly master a knowledge point, the best way is to use your own words to understand the content, and began to integrate the previous zero-scattered things, and improve your friends.

Operating array

There are many methods in the middle of the impression, the system is organized, put it in their own home, convenient to look back ~

Array.map ()

This method is to call each element in the array to provide a function, and the result is returned as a new array without changing the original array

  Let Arr = [1, 2, 3, 4, 5] let NEWARR = Arr.map (x => x * 2) // Arr = [1, 2, 3, 4, 5] The original array remains unchanged // NEWARR = [2, 4, 6, 8, 10] Return to the new array  

Array.Foreach ()

This method is to perform the function of each element in the array, no return value, directly change the original array, pay attention to the MAP method to distinguish

Let Arr = [ 1, 2, 3, 4, 5] Num.Foreach (x => x * 2) // Arr = [2, 4, 6, 10] array changes, pay attention to Map distinguishes
   
Array.Filter ()
This method is to judge all elements, and return the elements that satisfy the condition as a new array

Let ArR = [1, 2, 3, 4, 5] const isbigenough => value => value> = 3 let NEWARR = arr.filter (isbigenough) // newnuM = [3, 4, 5] Elements that meet the conditions return to A new array

Array.every ()
  This method is to determine all elements to return a boolean value, if All elements meet the judgment conditions, returns True, otherwise false:  

Let Arr = [1, 2, 3, 4, 5] const islessthan4 => value = > value value => value

Array.Some ()

This method is to determine all elements to return A Boolean value, if there is an element satisfying the judgment condition, return TRUE, if all elements do not meet the judgment conditions, return false:
   < 6
  arr.every(isLessThan4 ) //false
  arr.every(isLessThan6 ) //true 
Let Arr = [1, 2, 3, 4, 5] const islessthan4 => value => value value => value> 6 arr.some (islessthan4) // true arr.some // false

Array.Reduce ()

This method is that all element call returns functions, the return value is the final result, the incoming value must be a function type:
   let arr = [1, 2, 3, 4, 5] const add = (a, b) => a + b let sum = arr.reduce (add) // sum = 15 corresponds to the cumulative effect Correspondingly, there is a Array.reduceRight () method, the difference is that this operation is from right to left  

Array.push ()

this method is added after the newly added elements of the array, this method of changing the length of the array:

Array.pop ()
   this method removes the back end of array element, and returns an array, this method of changing the length of the array: 

let arr = [1, 2, 3, 4, 5 ] arr.pop () console.log (arr) // [1, 2, 3, 4] console.log (arr.length) // 4

Array.shift ()

this method removes the first element of the array in the back, and returns an array, this method of changing the length of the array:

  let arr = [1, 2, 3, 4, 5] arr.shift () console.log (arr) // [2, 3, 4, 5] console.log (arr.length) // 4  

Array.unshift ()

this method is to add one or more elements to the beginning of the array, and returns the new length of the array:

  Let Arr = [1, 2, 3, 4, 5] Arr.unshift (6, 7) Console.log (Arr) // [6, 7, 2, 3, 4, 5 ] Console.log (arr.length) // 7  
Array.isArray ()

Judging an array of objects Returns Boolean value

Array.concat ()

This method is a plurality of arrays to be spliced ​​into an array:
   Let Arr1 = [1, 2, 3] Arr2 = [4, 5] let arr = arr1.concat (arr2) console.log (arr) // [1, 2, 3, 4 , 5] 
Array.toString ()

This method converts the array into a string:

Let Arr = [1, 2, 3, 4, 5]; Let str = arr.tostring () Console.log (STR) // 1, 2, 3, 4, 5

Array.Join ()

This method also converts the array into a string:

 Let Arr = [1, 2, 3, 4, 5]; Let str1 = arr.tostring () let str2 = arr.tostring (',') Let STR3 = arr.tostring ('##') Console.log (str1) // 12345 console.log (str2) // 1, 2, 3, 4, 5 console.log (str3) // 1 ## 2 # # 3 ## 4 ## 5   
The difference between and TOSTRING can be seen, and the interval between elements can be set ~

Array.SPLICE (number of starting position, deletion, element)

If you can achieve, you can achieve modulation modification:

 Let Arr = [ 1, 2, 3, 4, 5]; Let Arr1 = Arr.Splice (2, 0 'Haha') Let Arr2 = Arr.Splice (2, 3) Let Arr1 = Arr.Splice (2, 1 'Haa) Console.log (Arr1) / / [1, 2, 'Haha', 3, 4, 5] Add an element console.log (arr2) // [1, 2] to delete three elements console.log (Arr3) // [1, 2, 'Haha', 4, 5] replace one element   
The first write is very simple Array Usage, the journey of a thousand miles begins with a single step, re-putting the foundation, if there is an error or misconduct, it is also known to inform THX

, which is a small-knit to everyone. Summarize the detailed integration, I hope to help everyone. If you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!

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

Please log in to comment