This is the 28th day of my participation in Gwen Challenge

Array traversal

We usually use a for loop to iterate over a number of groups. JS also provides forEach, which supports browsers above IE8.

  • ForEach () takes a function as an argument, and such functions that we create but do not call are called callback functions.

  • A few more elements in the array, and the function executes a few times, and each time, the browser passes in the elements as arguments, and we can define the parameters and read the contents

  • The browser passes three arguments in the callback

    (1) The first argument is the element currently standing in the traversal

    (2) The second argument is the index currently being traversed

    (3) The third array being traversed

var arr = [1.2.3.4.5.6]
arr.forEach(value, index, obj) {
   console.log(value)
}
Copy the code

2. Array deduplication

(1) Get each element in the array. The second loop gets all the elements following the current array

Gets each element in the arrayfor(var i=0; i<arr.length; i++) {
   // Get all elements after the current element
    fo(j=i+1; j<arr.length; j++) {
    // Determine whether the values of the two elements are equal
     if (arr[i] == arr[j]) {
         // delete the element corresponding to j
         arr.splice(j, 1)
         // When the current j element is deleted, the following element is automatically added
         // The element will no longer be compared, and the element at j will need to be compared again
         j--
     }
    }
}
Copy the code

3. Array join

(1) Concat () can concatenate two or more arrays and return a new array without affecting the group of elements

var arr= [12.34.5]
var arr2 = [6.7.8.9]
var arr3 = [6.7.8.9]
var result = arr.concat(arr2, arr3, '99'.'100')
Copy the code

(2) Join () array to string

This method does not affect the original array, but returns the converted string as the result,

Join () can also specify a string as an argument, which will be the concatenation of the elements in the array. If no concatenation is specified, the default is,

var arr= [12.34.5]
varResult = arr. Join (-)Copy the code

4. Array sort

(1) Sort () can be used to sort the elements of an array, affecting groups of elements. The default sort is Unicode

(2) So, even for numeric arrays, if you sort by sort(), you sort by Unicode, so the result is likely to be wrong.

(3) We can specify the collation by adding a callback function to sort () and specifying the collation

(4) The callback function in sort() takes two parameters. The browser calls the callback function using the elements in the array as arguments. The browser sorts the elements according to the return value of the callback function as follows:

  • If a value greater than 0 is returned, the elements swap places
  • If a value less than 0 is returned, the element position remains the same
  • If a 0 is returned, the two elements are considered equal and the positions are not swapped
var arr = [1.3.55.77.222.44.77.0] 
arr.sort(function(a, b) {
    if (a>b) {
       return 1
    } else {
        return -1;
    } else {
       return 0}) arr.sort(function(a, b) {
     / / l
    return a -b;
    / / descending
    // return b - a 
})
Copy the code

5, call/apply

Call () and apply() are both function object methods that need to be called through function objects.

(1) Function execution is called when both call() and apply() are called

Function fun() {console.log(' I'm a fun function ')} fun.call() fun.apply() fun() // all the above functions will be executedCopy the code

The difference is that call() and apply() can pass an object as an argument. In this case, the object will be called this when the function is executed

Function fun() {console.log(this)} var obj = {name:' ZLM '} fun()} window fun.call(obj) func.apply(obj) // ObjectCopy the code

(3) The call() method can pass arguments to an object only once

Fun. Call (abj, 2, 3)Copy the code

(4) The apply() method needs to wrap the arguments into an array to pass

Fun. Apply (obj, [1, 2, 3])Copy the code

Note:

  • When called as a function, this is always window
  • Called as a method, this is the object on which the method is called
  • Called as a constructor, this is the newly created object
  • Called with call and apply, this is the object specified

5, arguments

When calling a function, the browser passes two implicit arguments each time

The first is the function’s context object, this

The second is: wrapped argument object: arguments

Arguments is an array-like object that can also manipulate data by index or get length,

Arguments. length can be used to get the length of the argument

function fun() {
    console.log(arguments.length)
}
​
Copy the code

Note: Arguments has a property called callee

This property corresponds to a function object, the object of the function to which it is currently pointing

6. Class array objects convert arrays

(1) Array.from()

(2) Array.prototype.slice.call()

The slice() method of arrays returns a new array from an existing array. It takes two arguments, arr.slice(start,end). The first argument specifies where to start, the second argument specifies where to end (excluding), and returns a copy of the array if not passed. This method does not modify the original array. Instead, it returns the new array, which is used to convert an array into an array object.Copy the code

(3) Array. The prototype. The splice. Call (class Array, 0)

The splice(start,count,item) method takes different arguments. The first argument represents the position of the first element to be selected, and the second argument indicates the number of elements to be selected. The third parameter represents the element to be added (it will be added to the first parameter bit)Copy the code

(4) Extended operators

// Class array: Arguments object
function fun() {
    var arr = [...arguments]
}
/ / the NodeList object
[...document.querySelectorAll('div')]
Copy the code

(5) Loop through the class array object and push it into the newly created array object

var length = arrayLike.length;
var arr = [];
for (var i = 0; i < length; i++) {
    arr.push(arrayLike[i]);
    return arr;
}
Copy the code