Search code examples
javascripttaffydb

taffydb javascript object value from variable


I have the folllowing TaffyDB database:

var entities = TAFFY([{id:"0",name:"name1",pos_x:"200",pos_y:"200"},{id:"1",name:"name2",pos_x:"200",pos_y:"400"}]);

and would like to loop through picking up the x_pos of each database row in a variable.

This is what I got so far:

var entities_ID_array = Array();
entities_ID_array = entities().select("id");

for(i=0; i<entities_ID_array.length; i++){

        var pos_x = entities({id:i}).select("pos_x");
        alert(pos_x);

}

The alert gives empty results. If I change {id:i} to {id:0} and {id:1} is picks up the corresponding pos_x. Hence the difficulty I'm having seems to be the fact that I want to refer to a variable. The [i] notation doesn't seem to help either.

Who can help?


Solution

  • You didn't declare your variable i = I've edited it for you below:

    var entities = TAFFY([{id:"0",name:"name1",pos_x:"200",pos_y:"200"},{id:"1",name:"name2",pos_x:"200",pos_y:"400"}]); and would like to loop through picking up the x_pos of each database row in a variable.

    This is what I got so far:

    var entities_ID_array = Array(); entities_ID_array = entities().select("id");

    for(var i=0; i

        var pos_x = entities({id:i}).select("pos_x");
        alert(pos_x);
    

    }