Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. /*
  2. Spread Operator
  3. */
  4.  
  5. // Add the elements of an existing array into a new array
  6.  
  7. var certsToAdd = ['Algorithms and Data Structures', 'Front End Libraries'];
  8. var certifications = [
  9. 'Responsive Web Design',
  10. ...certsToAdd,
  11. 'Data Visualization',
  12. 'APIs and Microservices',
  13. 'Quality Assurance and Information Security'
  14. ];
  15. console.log(certifications);
  16.  
  17. // Pass elements of an array as arguments to a function
  18.  
  19. function addThreeNumbers(x, y, z) {
  20. console.log(x+y+z)
  21. }
  22. var args = [0, 1, 2, 3];
  23. addThreeNumbers(...args);
  24.  
  25. // Copy arrays
  26.  
  27. var arr = [1, 2, 3];
  28. var arr2 = [...arr]; // like arr.slice()
  29. arr2.push(4);
  30. console.log(arr);
  31. console.log(arr2);
  32.  
  33. // Concatenate arrays
  34.  
  35. var arr1 = [0, 1, 2];
  36. var arr2 = [3, 4, 5];
  37. //arr1.concat(arr2);
  38. arr1 = [...arr1, "JavaScript", ...arr2];
  39. console.log(arr1);
  40.  
  41. /*
  42. Rest Operator
  43. */
  44.  
  45. //Condense multiple elements into an array
  46.  
  47. function multiply(multiplier, ...theArgs) {
  48. //theArgs behave as Array so we can apply all Array methods
  49. //But it has to be the LAST(!) one specified, since it captures the rest of your arguments. 👏
  50. return theArgs.map(function(element) {
  51. return multiplier * element;
  52. });
  53. }
  54.  
  55. var arr = multiply(2, 1, 2, 3);
  56. console.log(arr)
  57.  
  58. // Delete duplicates & convert to arrayConvert to Array
  59.  
  60. let nums = [1, 2, 2, 3];
  61.  
  62. console.log([...new Set(nums)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement