Iterative method of array

Posted by FuriousIrishman on Sun, 26 Sep 2021 03:37:12 +0200

Higher order function

Array.forEach()

Array.map()          

Array.filter()

Array.reduce()

Array.reduceRight()

Array.every()

Array.some()

Array.indexOf()

Array.lastIndexOf()

Array.find()

Array.findIndex()

 Array.forEach()

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

be careful:   forEach() empty array will not execute the function   The loop cannot be ended.

        // Instead of the for loop, traverse the array
        var numbers = [45, 4, 9, 16, 25];
        numbers.forEach(function (value, index, array) {
            // value array, index array, arr original array
            console.log(value); // Output each in the array
        });

 Array.map() 

The map() method returns a new array. The elements in the array are the values of the original array elements after calling the function.

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

be careful:   map() does not detect empty arrays.

be careful:   map() does not change the original array.

        var numbers1 = [45, 4, 9, 16, 25];
        var numbers2 = numbers1.map(function (value) {
            return value * 2;   // Process the value of the array and return the processed value
        });
        console.log(numbers2); // Get the processed array

Array.filter()

The filter() method creates a new array. The elements in the new array are determined by specifying all the qualified elements in the array.

be careful:   filter() does not detect empty arrays.

be careful:   filter() does not change the original array.

        var numbers = [45, 4, 9, 16, 25];
        var over = numbers.filter(function (value) {
            return value > 18; // Filter those greater than 18 and return to the new array
        });
        console.log(over); // Filtered results

Array.reduce()

The reduce() method receives a function as an accumulator. Each value in the array (from left to right) is reduced and finally calculated as a value.

The reduce() method is used to operate, merge arrays, and return new arrays. You can transform multi-dimensional arrays into one-dimensional arrays

reduce() can be used as a high-order function to compose functions.

be careful:   reduce() does not execute callback functions for empty arrays.

reduce()     operation

        var numbers = [45, 4, 9, 16, 25];
        // total is the first value for the first time, and after the second time, it is the combined value or the calculated value
        // value normally traverses the array at one time
        var sum = numbers.reduce(function(total, value){
            return total + value; 
        });
        console.log(sum); // Result after operation

reduce()     merge

        var arr = [[10, 20, 30], ['hello'], [50, 100], [40], [70, 11, 22]];
        //Transform an array into a one-dimensional array
        var arrx = arr.reduce(function (total, value) {
            return total.concat(value) // concat merge array
        })
        console.log(arrx); // Results after merging into one dimension

Array.reduceRight()

Functions and of reducereight() method   reduce()   The function is the same, except that reduceRight() accumulates the array items in the array from the end of the array to the front.

be careful:   reduce() does not execute callback functions for empty arrays.

Array.every()

The every() method is used to detect whether all elements of the array meet the specified conditions (provided by the function).

The every() method detects all elements in the array using the specified function:

  • If an element in the array is detected to be unsatisfied, the entire expression returns   false  , And the remaining elements will not be detected.
  • Returns true if all elements meet the criteria.

be careful:   every() does not detect empty arrays.

be careful:   every() does not change the original array.

        var numbers = [45, 4, 9, 16, 25];
        var allOver = numbers.every(function(value){
            return value > 18; // false
            // return value > 3; // true
        });
        console.log(allOver);

Array.some()

The some() method is used to detect whether the elements in the array meet the specified conditions (provided by the function).

some() each method will enumerate the following elements in detail:

  • If an element satisfies the condition, the expression returns true  , The element of the element no longer performs detection.
  • If there is no element that meets the condition, false is returned.

be careful:   some() does not detect empty arrays.

be careful:   some() does not change the original array.

        var numbers = [45, 4, 9, 16, 25];
        var allOver = numbers.some(function(value){
            return value > 18; // true
            // return value > 3; // false
        });
        console.log(allOver);

Array.indexOf()

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

If no matching string is found, - 1 is returned.

be careful:   The indexOf() method is case sensitive.

Tips:   Similarly, you can see similar methods   lastIndexOf() .

        var fruits = ["Apple", "Orange", "Apple", "Mango"];
        var a = fruits.indexOf("Apple"); // Find Apple's position in the array
        console.log(a); // Result 0

Array.lastIndexOf()

The lastIndexOf() method can return the last position of a specified string value. If the second parameter start is specified, the specified position in a string will be searched from back to front.

be careful:   This method retrieves the string from the back to the front, but returns the last occurrence of the substring starting from the starting position (0). See if it contains a string.

The start of retrieval is at the start of the string or at the end of the string (when start is not specified).

If no matching string is found, - 1 is returned.

Note: the lastIndexOf() method is case sensitive!

Tips:   You can also refer to similar methods   indexOf() .

        var fruits = ["Apple", "Orange", "Apple", "Mango"];
        var a = fruits.lastIndexOf("Apple"); // Find Apple's position in the array
        console.log(a); // Result 2

Array.find()

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

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

  • Returns true when the elements in the array test the condition   When, find() returns the qualified element, and the subsequent value will no longer call the execution function.
  • undefined if there are no qualified elements

be careful:   find() for an empty array, the function does not execute.

be careful:   find() does not change the original value of the array.

        var numbers = [4, 9, 16, 25, 29];
        var first = numbers.find(function (value) {
            return value > 18; // Find one that meets the requirements and it's over
        });
        console.log(first); // 25

Array.findIndex()

The findIndex() method returns the position of the first element of the array passing in a test condition (function) that meets the condition.

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

  • Returns when the elements in the array are in the test condition   true   When, findIndex() returns the index position of the qualified element, and the subsequent value will not call the execution function.
  • If there are no qualified elements, - 1 is returned

be careful:   findIndex() for empty arrays, the function will not execute.

be careful:   findIndex() does not change the original value of the array.

        var numbers = [4, 9, 16, 25, 29];
        var first = numbers.findIndex(function (value) {
            return value > 18; // Found an index location that meets the requirements
        });
        console.log(first); // 3

Topics: PHP