Travase Objects and four method of array

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>

<script>

    var iterableObj = {
        "lastName":'luo',
        "firstName":'xu',
        "age":19
    }
    let keys = Object.keys(iterableObj); //return array of keys in the iterableObj
   // console.log(keys) //Array(3) [ "lastName", "firstName", "age" ]
    let vals = Object.values(iterableObj);
    //console.log(vals) //["luo","xu",19]
    let  entries = Object.entries(iterableObj)
    //console.log(entries)  //[["lastName", "luo"],[ "firstName", "xu"],["age", 19]]  2-D arrays

    const companies = [ //array
        {name: "Company One", category: "Finance", start: 1981, end: 2003}, //object
        {name: "Company Two", category: "Retail", start: 1992, end: 2008},
        {name: "Company Three", category: "Auto", start: 1999, end: 2007},
        {name: "Company Four", category: "Retail", start: 1989, end: 2010},
        {name: "Company Five", category: "Technology", start: 2009, end: 2014},
        {name: "Company Six", category: "Finance", start: 1987, end: 2010},
        {name: "Company Seven", category: "Auto", start: 1986, end: 1996},
        {name: "Company Eight", category: "Technology", start: 2011, end: 2016},
        {name: "Company Nine", category: "Retail", start: 1981, end: 1989}
    ];

    const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];

//1. forEach()
    for (let i = 0; i < companies.length; i++){
       // console.log(companies[1]);
    }

    companies.forEach(function (company, index , companies) { //we can pass 2 params
           // console.log(company);
    });

//2. filter()
    //store the person'age over 21, who can drink

    var canDrink = [];
    for (let i=0; i< ages.length ; i++){
        if (ages[i] >= 21){
            canDrink.push(ages[i]);
        }
    }

    canDrink = ages.filter(function (age) {
            if (age >= 21){
                return true;
            }
    });

    canDrink = ages.filter(age => age >=21);

    console.log(canDrink);

    //get companies that lasted ten years or more
    const lastedTenYears = companies.filter((company, index) => (company.end - company.start >=10));
    console.log(lastedTenYears);

//3. map()
    var testMap = companies.map(function (company,index ) {
            return company.name;
    });  //Array(9) [ "Company One", "Company Two", "Company Three", "Company Four", "Company Five", "Company Six", "Company Seven", "Company Eight", "Company Nine" ]

     testMap = companies.map(function (company ) {
        return `${company.name } [${company.end} - ${company.start}]`;
    }) //Array(9) [ "Company One [2003 - 1981]", "Company Two [2008 - 1992]", "Company Three [2007 - 1999]", "Company Four [2010 - 1989]", "Company Five [2014 - 2009]", "Company Six [2010 - 1987]", "Company Seven [1996 - 1986]", "Company Eight [2016 - 2011]", "Company Nine [1989 - 1981]" ]

    console.log(testMap)

//4. sort()
    //sort by start year
    var sorted = companies.sort(function (a,b ) {
        if (a.start > b.start){
            return 1;
        }else {
            return -1;
        }
    });

    sorted = companies.sort((a,b) => a >b ? 1 : -1);

    //sort ages
    sorted =ages.sort((a,b) => a-b);
     console.log(sorted);

//5. reduce()

    var total = ages.reduce(function (total, age) {
        return total+age;
    });
    total = ages.reduce((total, age) => total +age);

    console.log(total);


</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/luoxuw/p/12362086.html