Javascript Data Type How to - Add object to arrays storing values








Question

We would like to know how to add object to arrays storing values.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from   w  w w. jav  a 2s.c o m-->
var parray = [];
for(counter = 0; counter < 10; counter++){
  parray.push({
    id : counter,
    isAvailable : 0
  });
}
for (var idx = 0; idx < parray.length; idx++) {
  var pt = parray[idx];
  document.writeln(JSON.stringify(pt));
}

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: