Page 96 - JavaScript
P. 96

let odd = numbers.filter(n => n % 2 !== 0); // can be shortened to (n => n % 2)


        odd would contain the following array: [5, 43].


        It also works on an array of objects:


         var people = [{
           id: 1,
           name: "John",
           age: 28
         }, {
           id: 2,
           name: "Jane",
           age: 31
         }, {
           id: 3,
           name: "Peter",
           age: 55
         }];


        5.1

         var young = people.filter(function(person) {
           return person.age < 35;
         });


        6


         let young = people.filter(person => person.age < 35);


        young would contain the following array:


         [{
           id: 1,
           name: "John",
           age: 28
         }, {
           id: 2,
           name: "Jane",
           age: 31
         }]


        You can search in the whole array for a value like this:


         var young = people.filter((obj) => {
           var flag = false;
           Object.values(obj).forEach((val) => {
             if(String(val).indexOf("J") > -1) {
               flag = true;
               return;
             }
           });
           if(flag) return obj;
         });





        https://riptutorial.com/                                                                               53
   91   92   93   94   95   96   97   98   99   100   101