create an object array with an iteration while

Chahmeldine :

I would like to insert each new iteration of value of an array in the form of an object. What am I missing?

x = []
var min=0
var interval = 50
var max = 500

  while(min  < max) {
    min = min+interval,
    x = {

      start: min,
      end: min+interval,
      duration : interval
  
    }
    
    console.log(x)
  }
x = []
var min=0
var interval = 50
var max = 500

  while(min  < max) {
    min = min+interval,
    x = {

      start: min,
      end: min+interval,
      duration : interval
  
    }
    
    console.log(x)
  }

Expected result :

[
 { min: 0,
   max: 50,
   duration: 50
}
 { min: 50,
   max: 100,
   duration: 50
}

]
 ect..
T.J. Crowder :

In JavaScript, assigning to the array variable (x = ...) does not push to the array like it does in a couple of other languages. It replaces the value of the variable (x) with what's on the right-hand side.

To push to the array, use its push method:

x.push({/*...*/});

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405734&siteId=1