javascript - Iterating through an arrays properties methods in js -
i have array of objects , i'm trying iterate through each object in array. need call method on each object. how should go doing this?
function basicbox(x,y,width,height,color) {     this.color = color;     this.x = x;     this.y = y;     this.width = width;     this.height = height;     this.draw = function() {         g.drawstyle = this.color;         g.drawrect(this.x,this.y,this.width,this.height);     } }  var boxes = [b1,b2,b3];  function run() {     (var = 0; < boxes.length; ++i) {         boxes[i].update();     } } 
it think array.map(), array.reduce() , of course array.foreach() can can stuff asking for. using for or while loops absolutely adequate iterate arrays, worth try more functional approach, because prevents rewriting loops , lets focus on task, should done each element.
if iterate on properties of object, for in loop possible, there object.keys() returns properties of given object.
Comments
Post a Comment