Help understanding properties

I’m trying to understand the behavior of properties in this function. I’ve created entirely by accident, or rather I have not read any documentation prior to coding this. I know vaguely how properties work, but the behaviour displayed here seems unusual. Although the code does produce the information I’m looking for it is in fact nested in many arrays and I dont understand why.

Here is the code.

function mapPath2(){

if(pathfind2.toggle===1){

for(var i=nodestar.length-1;i>-1;i--){
    // if(backupstar[i].original_id){
    var b = backupstar[i].original_id;
    nodestar[i].imprinted_by[b] = true;

    var next = nodestar[i].neighbourNode(true,b,backupstar[i],starhist[i]);

    nodestar[i].highlight();
    if(next){
        next[1].node_to_node_dist[b] = nodestar[i].node_to_node_dist[b] + next[0];
        backupstar[i].nodesconnected[next[1].original_id] = next[1].node_to_node_dist[b];
        //next[1].path_ref[b] = [];
        if(!nodestar[i].path_ref[b]){
        //next[1].path_ref[b] = [nodestar[i]];
        nodestar[i].path_ref[b] = []
        }
        else{

        }
        //starhist[i].push([nodestar[i].path_ref[b],next[1]])
        next[1].path_ref[b] = [nodestar[i].path_ref[b],next[1]];
        backupstar[i].pathtable[next[1].original_id] = next[1].path_ref[b];
        starhist[i].push(nodestar[i]);


        stackstar[i].push(nodestar[i]);

        trimStackStar(stackstar[i],b);
        nodestar[i] = next[1];

      }

    else if(!next&&stackstar[i].length>0){

      nodestar[i] = stackstar[i].pop();
      //starhist[i] = stackstar[i]
    }
    else{
      //console.log(i)
      backupstar.splice(i,1);
      nodestar.splice(i,1);
      stackstar.splice(i,1);
      starhist[i].splice(i,1);
      //i += -1;
    }
  }}

};

Its a function made to identify the ideal path between two points which it does however the cells are deeply nested rather than being flat. And I tried to use the push function instead to no avail.

22%20AM

1 Like

this was solved using the flatten function, next[1].path_ref[b] = next[1].path_ref[b].flat(); is used before backupstar[i].pathtable is updated, and therefore the finished array is flat.

1 Like