Advertisement
fcamuso

Javascript Lezione 68

Aug 15th, 2022
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.   <title>Spread Rest</title>
  8. </head>
  9. <body>
  10.   <script>
  11.     const arr = [23, -9, 15, 67];
  12.  
  13.     //SPREAD OPERATOR
  14.     //con collezioni enumerabili (iterable) come stringhe, array, map, set, oggetti
  15.     //  [23, -9, 15, 67]  =>  23, -9, 15, 67  
  16.  
  17.     //copia di array
  18.     let copia = [...arr];
  19.     writeln(copia);
  20.  
  21.     let arr2 = [[1,2], [3,4]];
  22.     copia = [...arr2];
  23.     copia[1][0] = 33;
  24.     writeln(copia);
  25.     writeln(arr2);
  26.  
  27.     const arr3 = [{obj1: 'ciao'}, {obj2: 'a tutti'}];
  28.     copia = [...arr3];
  29.     copia[0].obj1 = 'marameo';
  30.     writeln(arr3[0].obj1);
  31.  
  32.     arr2 = [100, 200, 300, 400];
  33.     const fusione  = [...arr, ...arr2];
  34.     writeln(fusione);
  35.  
  36.     //stringhe
  37.     const s='sono una stringa'
  38.     const arrChar = [...s];
  39.     writeln(arrChar);
  40.  
  41.     //oggetti
  42.     let cliente = {cognome: 'picchi', nome: 'Giulio'};
  43.     cliente = {...cliente, codice_fiscale: 'xxxxx'};
  44.     console.log(cliente);
  45.  
  46.     function MediaDi3(n1, n2, n3)
  47.     {
  48.       return  (n1 + n2 + n3)/3;
  49.     }
  50.  
  51.     arr2 = [10, -19, 78, 56, 0, 345];
  52.     writeln(MediaDi3(...arr2.slice(0,3)));
  53.  
  54.     //rest, a sinistra dell'operatore di assegnamento
  55.     let [ele1, ele2, ...ilResto] = arr2;
  56.     writeln(ele1);
  57.     writeln(ele2);
  58.     writeln(ilResto);
  59.  
  60.     let {cognome, ...ilRestoObj} = cliente;
  61.     console.log(cognome);
  62.     console.log(ilRestoObj);
  63.  
  64.     function somma(...numeri)
  65.     {
  66.        let risultato=0;
  67.        numeri.forEach( ele => risultato+=ele );
  68.        return risultato;
  69.     }
  70.  
  71.     writeln(somma(1,6,8));
  72.     writeln(somma(1,6,8, 56, 23, -51));
  73.  
  74.     writeln(somma(...[34, 67, 76]));
  75.  
  76.  
  77.     function writeln(messaggio, cornicetta=false)
  78.     {
  79.       document.write(messaggio+"<br>");
  80.       if (cornicetta)
  81.         writeln("-".repeat(40));
  82.     }
  83.    
  84.   </script>
  85. </body>
  86. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement