Page 106 - JavaScript
P. 106

var array = [];
         for(var people in object) {
           array.push([people, object[people]]);
         }


        Now array is


         [
           ["key1", 10],
           ["key2", 3],
           ["key3", 40],
           ["key4", 20]
         ]



        Sorting multidimensional array


        Given the following array


         var array = [
           ["key1", 10],
           ["key2", 3],
           ["key3", 40],
           ["key4", 20]
         ];


        You can sort it sort it by number(second index)


         array.sort(function(a, b) {
           return a[1] - b[1];
         })


        6


         array.sort((a,b) => a[1] - b[1]);


        This will output


         [
           ["key2", 3],
           ["key1", 10],
           ["key4", 20],
           ["key3", 40]
         ]


        Be aware that the sort method operates on the array in place. It changes the array. Most other
        array methods return a new array, leaving the original one intact. This is especially important to
        note if you use a functional programming style and expect functions to not have side-effects.


        Removing items from an array


        Shift




        https://riptutorial.com/                                                                               63
   101   102   103   104   105   106   107   108   109   110   111