0

I have an array something like this:


let arr = [
   {
     num: 10,
     reload: () => {
         this.num = 30;
     }

   },
   {
     num: 40,
     reload: () => {
         this.num = 20;
     }

   }
]

then I am trying to run this code

arr.forEach(item => {
   item.reload()
})

I want the array's first object to have num = 30 and second one as num = 20

this doesn't seen to work..

Nika Kurashvili
  • 6,006
  • 8
  • 57
  • 123

1 Answers1

3
let arr = [
  {
    num: 10,
    reload: function(){
      this.num = 30;
    }

  },
  {
    num: 40,
    reload: function(){
      this.num = 20;
    }

  }
]


arr.forEach(item => {
  item.reload()
})

console.warn(arr);
Jafar Jabr
  • 642
  • 5
  • 11