Javascript Array notes summary

For each map mapping array, there the return value

 

 

map definitions and methods 
map () method returns a new array, the array element calls a function of the value of the array element to the original processing. 
map () method in the original order of the processing elements in the array elements. 
Note: 
map will not detect an empty array 
map does not change the original array 
arr.map (function (currentValue, index,  arr), thisValue)
Parameter Description 
function (currentValue, index, arr) 
each have, function, array this function will perform the elemental function parameter 
arguments 
currentValue current element must value 
index An optional index value of the current element 
arr optional array of objects belonging to the current element. 

Score = the let [19,58,80,97 ]; 
 the let Result = score.map ( function () {
      return Item> = 60 'pass':? 'fail' ; 
 })
  -> Result = [fail, not pass, pass, pass];
ARR = the let [1,2,3,4 ]; 
 the let Result = arr.map ( function (Item) {
      // Item Each array is a 
     return Item * 2 ; 
 });
  -> Result = [. 1, 9, 16]

forEach loop and for the same drops, no return value, that is, for circulation for a way to kind of write

let arr=[12,12,33,45];
 arr.forEach(function(item,index){
     console.log(item);
     -> 12,12,33,45
 })

filter filter (left part, disappeared part)

// by returning true or false to decide whether to return out 
 // example: Returns the number divisible by 3 of         
 the let ARR = [12,5,8,9 ]; 
 the let Result = arr.filter ( function (Item) {
      IF (% Item. 3 == 0 ) {
          return  to true ; 
     } the else {
          return  to false 
     } 
     // abbreviated == 0. 3% Item return;
})

reduce Summary (out of a pile of) the total number of calculations, calculates the average of more strenuous

//求和
 let arr=[11,122,333,344];
 let result=arr.reduce(function(tmp,item,index){
     return tmp+item;
 })
  • Such processes are droplet: If we want to calculate an adder, 122 + 333 + 11 + 344; 11 + we have to calculate a first step 122, 133 to obtain an intermediate result, the intermediate result is then added to 333, and then will give an intermediate result 466, and then the final result by 466 to 810 plus 344
  • Reduce operating principle is also the case, since the first intermediate result is not started, so the array is assigned tmp first item is the second item in the array, index is 1, then calculates 11 + 122, 133 return the results obtained out, when the second calculation result is tmp + 12 122 133, item is in the third array 333, index 2, the results tmp + item 466 returns out; tmp is calculated at the third 466, item is 344, index is 3, and the results returned to go out, because this is the last item in the array, so the results back to the result.
The number of calculations tmp item index
0th 11 122 1
1st 11+122 333 2
2nd (11+122)+333


  // calculate the mean average of the last claim 
        // summing 
        the let ARR = [11,122,333,344 ]; 
        the let Result = arr.reduce ( function (TEMP, Item, index) {
             IF ! (Index = arr.length) { // not the last time, summed 
                return the TEMP + Item; 
            } the else { // the last 
                return (the TEMP + Item) /arr.length;
             } 
        })
 * application: calculate the total price of the shopping cart

String.split () operation performed  Array.join  operation performed is reversed.

<- Example:! If there is a list of goods in a shopping cart -> 
 the let shoppingCart = [ 
     {ID: . 1, name: 'sock',. Price: 56 is, imgs: '1.jpg, 2.jpg' }, 
     ID {: 2, name: 'underwear',. price: 156, imgs: '3.jpg, 4.jpg' }, 
     {ID: . 3, name: 'jacket', price: 1156, imgs: '5.jpg, 6.jpg ' }, 
     {ID: . 4, name:' pants',. price: 4156, imgs: '7.jpg, 8.jpg' }, 
     {ID: . 5, name: 'bra', price: 14156, imgs : '9.jpg, 10.jpg' }, 
 ] 
 // 1. 1000 was filtered off clothes less than the price of 
 the let Result = shoppingCart.filter (Item => item.price> = 1000 );
  -> Result = [ 
     {ID: 3, name: 'jacket', price: 1156,imgs:'5.jpg,6.jpg'},
     {id:4, name: 'pants',. Price: 4156, imgs:' 7.jpg, 8.jpg ' }, 
     {ID: . 5, name:' bra ', price: 14156, imgs: ' 9.jpg, 10.jpg ' } 
 ] 
 // 2. array format image into 
 the let Result = shoppingCart.map (Item => { 
     item.imgs = item.imgs.split (', ' );
      return Item; 
 })
  -> Result = [ {id: 1, name: 'sock',. price: 56 is, imgs: [ '1.jpg', '2.jpg' ]}, 
     {ID: 2, name: 'underwear', price: 156, imgs: [ '3.jpg', '4.jpg' ]}, 
     {ID: . 3, name: 'jacket',. price: 1156, imgs: [ '5.jpg', '6.jpg' ]}, 
     {ID: . 4 , name:'Pants',. Price: 4156, imgs: [ '7.jpg', '8.jpg' 'Pants',. Price: 4156, imgs: [ '7.jpg', '8.jpg' ]},
     {ID:5, name: 'bra',. Price: 14156, imgs: [ '9.jpg', '10 .jpg ' ]}]
  // 3. First, the total price calculated using the map to map out the price, then reduce calculation and 

 let total_price = shoppingCart.map (Item => item.price) 
                             .reduce ((TEMP, Item) => Item TEMP +);

 Disrupt the order of the array (the essence of the idea is to change the position of the array)

            var shuffle = function(arr) {
                var len, t, rand;
                for (var i = 0; len = arr.length, i < len; i++) {
                    rand = parseInt(Math.random() * len); 
                    //parseInt(Math.random()*(len-1-0)+1);或者rand = Math.floor(Math.random()*(len-1-0)+1);即Math.random()*(Max-Min)+1
                    t = arr[rand];
                    arr[rand] = arr[i];
                    arr[i] = t;
                }
                console.log(arr);
             
            }
            shuffle([1,2,3,4,5,6]);

 

Guess you like

Origin www.cnblogs.com/ddqyc/p/10451202.html