Advertisement
JPDG13

Loops and Arrays, ES6

Apr 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const boxes = document.querySelectorAll('.box');
  2.  
  3. // ES5
  4. //var boxesArr5 = Array.prototype.slice.call(boxes);
  5. //boxesArr5.forEach(function(cur) { cur.style.backgroundColor = 'dodgerblue'});
  6.  
  7. // ES6
  8.  
  9. Array.from(boxes).forEach(cur => cur.style.backgroundColor = 'dodgerblue');
  10.  
  11. // Loops in ES6
  12.  
  13. for (const cur of boxes) {
  14.     if (cur.className.includes('blue')) {
  15.         continue;
  16.     }
  17.     cur.textContent = 'I changed to blue!';
  18. }
  19.  
  20. // ES 5
  21.  
  22. var ages = [12, 17, 8, 21, 14, 11]
  23.  
  24.  var full = ages.map(function(cur) {
  25.     return cur >= 18;
  26.  });
  27.  
  28.  console.log(ages);
  29. //A lof of work to get a true or false for ages
  30. console.log(full.indexOf(true));
  31. console.log(ages[full.indexOf(true)]);
  32.  
  33. // ES 6
  34. // of all the ages, find the index location where cur >= 18
  35. console.log(ages.findIndex(cur => cur >= 18));
  36.  
  37. console.log(ages.find(cur => cur >= 18));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement