JS-16-Array Advanced API

Posted by sagetim on Tue, 14 May 2019 12:13:30 +0200

1. On Array Advanced API

Note: Attributes in objects are out of order
For-in loops are designed to traverse objects, but the properties of objects are out of order, so for-in loops are designed to traverse out of order, so for-in loops are not recommended for traversing arrays.

1. Traverse the array using the for of loop introduced in ES6:

       for(let value of arr){
         console.log(value);
       }

2. Array objects can also be used to traverse arrays using the forEach method

The forEach method automatically calls the incoming function, and each call passes to it the elements currently traversed, the index currently traversed, and the array currently traversed.

arr.forEach(function (currentValue, currentIndex, currentArray) {
         // console.log(currentValue, currentIndex, currentArray);
         console.log(currentValue);
     });

2. In Array Advanced API

1. findIndex method for arrays
findIndex method: Custom version of indexOf, found return index, found return-1;

2. find method for arrays
The find method returns the index, and the find method returns the element found.
The find method returns the element found if it is found, or undefined if it is not found:

let value = arr.find(function (currentValue, currentIndex, currentArray) {
   // console.log(currentValue, currentIndex, currentArray);
   // if(currentValue === 6){
   if(currentValue === 10){
       return true;
   }
});
console.log(value);

3. Under Array Advanced API

1. filter method for arrays: add elements that meet the criteria to a new array;

  let newArray = arr.filter(function (currentValue, currentIndex, currentArray) {
            // console.log(currentValue, currentIndex, currentArray);
            if(currentValue % 2 === 0){  //Ask for even numbers
                return true;
            }
        });
   console.log(newArray); // [2, 4]

2. The map method of an array: Map elements that meet the criteria into a new array;

let newArray = arr.map(function (currentValue, currentIndex, currentArray) {
    // console.log(currentValue, currentIndex, currentArray);
    if(currentValue % 2 === 0){
        return currentValue;
    }
});
console.log(newArray); // [undefined, 2, undefined, 4, undefined]

3. Requirements: Delete all elements in the array while traversing;

let arr = [1, 2, 3, 4, 5];
for(let i = 0; i < arr.length; i++){
            console.log(arr.length);
            // Note: Delete elements from the array by delete, and the length property of the array will not change
            delete arr[i];
        }
        console.log(arr);

4. JavaScript-Array Sorting Method

let arr = ["c", "a", "b"];
        // arr.sort();

arr.sort(function (a, b) {
            if(a > b){
                return -1;
            }else if(a < b){
                return 1;
            }else{
                return 0;
            }
        });
        console.log(arr);

Rule: If an element in an array is a numeric type
Returns a - b if an ascending sort is required;
Returns b - a if a descending order is required;
// return a - b;
return b - a;

  • Sort by string length:
let arr = ["1234", "21", "54321", "123", "6"];
        arr.sort(function (str1, str2) {
            // return str1.length - str2.length;
            return str2.length - str1.length;
        });
        console.log(arr);
  • Sort by object age in the array:
let students = [
    {name: "zs", age: 34},
    {name: "ls", age: 18},
    {name: "ww", age: 22},
    {name: "mm", age: 28},
];
students.sort(function (o1, o2) {
    // return o1.age - o2.age;
    return o2.age - o1.age;
});
console.log(students);

-End

Topics: Javascript