Page 102 - JavaScript
P. 102

var arr = [4, 2, 1, -10, 9]

         arr.reduce(function(a, b) {
           return a < b ? a : b
         }, Infinity);
         // → -10


        6


        Find Unique Values


        Here is an example that uses reduce to return the unique numbers to an array. An empty array is
        passed as the second argument and is referenced by prev.


         var arr = [1, 2, 1, 5, 9, 5];

         arr.reduce((prev, number) => {
           if(prev.indexOf(number) === -1) {
             prev.push(number);
           }
           return prev;
         }, []);
         // → [1, 2, 5, 9]



        Logical connective of values


        5.1

        .some and .every allow a logical connective of Array values.


        While .some combines the return values with OR, .every combines them with AND.

        Examples for .some


         [false, false].some(function(value) {
           return value;
         });
         // Result: false

         [false, true].some(function(value) {
           return value;
         });
         // Result: true

         [true, true].some(function(value) {
           return value;
         });
         // Result: true


        And examples for .every


         [false, false].every(function(value) {
           return value;




        https://riptutorial.com/                                                                               59
   97   98   99   100   101   102   103   104   105   106   107