javascript - No access to an Object in an Array, but to the object itself -
i have following code, creating objects within loop , accessing values of them. came problem, accessing object ok, not array contains object. missing here?
camlist = new array();  (var i=0; i<8; i++) {     var camobj = new camera(i);     camlist.push(camobj);     console.log(camobj.id);     //this works     console.log(camlist[i].id); //this doesnt } ps: in example camobj.id returns current value of i.
pps: got typeerror: cannot read property 'id' of undefined.
edit:
i added full code example.
camera object:
var camera = function(id, cam) {     this.id = id;     this.cam = cam;     this.res =  {                     "x" : this.cam.get(cv_cap_prop_frame_width),                     "y" : this.cam.get(cv_cap_prop_frame_height)                 };     this.overlaydata = new array(); };  exports.camera = camera; main code:
var cv      = require("opencv-node"); var camera  = require("./cameramodule").camera; var camlist = new array();  (var i=0; i<8; i++) {     var capture = new cv.videocapture();     capture.open(i);      if (capture.isopened())     {         var camobj = new camera(i, capture);         camlist.push(camobj);         console.log(camlist[i].id); //here problem     } } 
in each loop iteration, checking capture.isopened().  happen if false?
the loop continue without being pushed camlist.  on next loop, i incremented.  let's capture.isopened() becomes true.  when insert array, i not line element added.  i 1 (or more) indexes big!
instead of doing camlist[i].id, try this:
camlist[camlist.length - 1].id 
Comments
Post a Comment