Here is an example of a topic on the web to analyze the array of several uses, you can print the results in the browser console for comparison. Two arrays are given here

  • Group 1 (first name, last name, date of birth, and date of death)
const inventors = [
    { first: 'Albert'.last: 'Einstein'.year: 1879.passed: 1955 },
    { first: 'wawa'.last: 'fs'.year: 1830.passed: 1905 },
    { first: 'grvd'.last: 'xcvxcv'.year:1900.passed: 1977 },
    { first: 'Hanna'.last: 'Hammarström'.year: 1829.passed: 1909}];Copy the code
  • The second group (the People array, which contains a list of names separated by commas.)
['Albert, Einstein'.'wawa, fs'.'grvd, xcvxcv'.'Hanna, Hammarström']
Copy the code
According to these two arrays, complete the following problems
  • Screening inventors born in the 16th century;
  • List the first and last names in an array.
  • According to their date of birth, and in order from most to least;
  • Calculate how many years all the inventors together lived;
  • In order of age;

Filter () (filter operation, filter all elements that meet the criteria, if true returns to form a new array, using the first header as an example 🙂

    function bornyear(inventor) {
      return inventor.year >= 1800 && inventor.year < 1900;
    }
    var fifteen = inventors.filter(bornyear);
    console.log(fifteen);
    // can be simplified as
    const fifteen = inventors.filter(inventor= > (inventor.year >= 1500 && inventor.year < 1600));
Copy the code

We start with a function called Bornyear, which filters for inventors born in the 19th century and returns true or false. Afterwards, the filter method is called to filter the elements in the nIA for whether the elements meet the screening conditions of bornyear function. Finally, an array of results matching the criteria is returned as follows.

[{first: "Albert".last: "Einstein".year: 1879.passed: 1955},
{first: "wawa".last: "fs".year: 1830.passed: 1905},
{first: "Hanna".last: "Hammarström".year: 1829.passed: 1909}]Copy the code

Map (A mapping operation that processes each element of the original array and returns the new array. Take subject 2 for example)

 const fullnames = inventors.map(inventor= > `${inventor.first} ${inventor.last}`);
 console.log(fullnames);
Copy the code

The printed result is:

["Albert Einstein"."wawa fs"."grvd xcvxcv"."Hanna Hammarström"]
// the nCII. map is followed by the combination of the nCII. map and nCII. map. The final array returned is a new array that has been processed
Copy the code

Sort (Sort operation. The default sort order is based on string Unicode code points, such as 10 before 2 and number before uppercase letter, uppercase letter before lowercase letter. You can also use a comparison function, where the array is sorted by the value returned from the call, in the following format.)

function compare(a, b) {
    if (a < b) {
    // By some sort of comparison, a is less than B
      return - 1;
    }
    if (a > b) {
      return 1;
    }
    // when a === b
      return 0;
}

Copy the code

To compare numbers instead of strings, the comparison function can simply be a minus b. The following function will sort the array in ascending order:

 function compareNumbers(a, b) {
    return a - b;
 }
Copy the code

For problem 3, we can simply compare plus and minus

 const birthdate = inventors.sort((inventora, inventorb) = > (inventorb.year - inventora.year));
console.log(birthdate)
Copy the code

The printed result is:

[{first: "grvd".last: "xcvxcv".year: 1900.passed: 1977},
{first: "Albert".last: "Einstein".year: 1879.passed: 1955},
{first: "wawa".last: "fs".year: 1830.passed: 1905},
{first: "Hanna".last: "Hammarström".year: 1829.passed: 1909}]Copy the code

Reduce () (merge operation, a total of two parameters, the first is the function, can be understood as an accumulator, iterate over the number of groups of cumulative return value, the second is the initial value. If no initial value is provided, the first element in the array is used. Take question 4 for example 🙂

const totalyears = inventors.reduce((total, inventor) = > { return total + (inventor.passed - inventor.year); }, 0);
console.log(totalyears);
Copy the code

Arrays can be conveniently filtered to find the values to add. It returns the sum