ivana_andreevska

JavaScript metodi

Jan 16th, 2022
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const people = [
  2.     {name: 'Wes', year: 1988},
  3.     {name: 'Kait', year: 1986},
  4.     {name: 'Irv', year: 1970},
  5.     {name: 'Lux', year: 2015},]
  6. const comments = [
  7.     {text: 'Love this!', id: 523423},
  8.     {text: 'Super good', id: 823423},
  9.     {text: 'You are the best', id: 2039842},
  10.     {text: 'Ramen in my fav food ever', id: 123523},
  11.     {text: 'Nice Nice Nice!', id: 542328}
  12. ];
  13.  
  14. //some method->is at least one person 19
  15. const isAdult=people.some(function (person){
  16.     const currYear=(new Date().getFullYear())
  17.     if(currYear-person.year>=19)
  18.     {
  19.         return true;
  20.     }
  21. })
  22.  
  23. const isAdult2=people.some(person=>{
  24.     const currYear=(new Date().getFullYear());
  25.     return currYear-person.year>=19;
  26. })
  27. //console.log(isAdult2)
  28.  
  29. //every method->is every 19
  30. const allAdults=people.every(function (person){
  31.     const currYear=(new Date().getFullYear())
  32.     if(currYear-person.year>=19)
  33.         return true;
  34. })
  35. console.log(allAdults)
  36.  
  37. const allAdults2=people.every(person=>
  38.     (new Date().getFullYear())-person.year>=19);
  39. // console.log(allAdults2)
  40.  
  41.  
  42. //find method->first item it finds
  43. const comment=comments.find(function (comment){
  44.     if(comment.id===823423)
  45.     {
  46.         return true;
  47.     }
  48. })
  49. // console.log(comment)
  50.  
  51. const comment2=comments.find(comment=>comment.id===823423);
  52. // console.log(comment2)
  53.  
  54. //findIndex
  55.  
  56. const index=comments.findIndex(comment=>comment.id===823423)
  57. console.log(index)
  58.  
  59. comments.splice(index,1)
  60. console.table(comments)
  61. const newComm=[
  62.  comments.slice(0,index),
  63.  comments.slice(index+1)
  64. ]
  65. console.table(newComm)
Advertisement
Add Comment
Please, Sign In to add comment