create object and sub object using javascript loop

create object and sub object using javascript loop

To create create object and sub-object using javascript loop use the below code snippet

var mainobj = [];
var count = 0;
 for (const key  of walkingdata) {
    
    mainobj.push({lat: key.lat, lng: key.lng,subdata:[]});
    
    for (i = 0; i < walkingdata.length; i++) {
       if(walkingdata[i].distance < 600){
        
        mainobj[count].subdata.push({lat: key.lat, lng: key.lng});
       }
       else{
        // mainobj[count].subdata.push({lat: key.lat, lng: key.lng});
       }
     }

     // mainobj[count].subdata.push({lat: key.lat, lng: key.lng});
    count++;
 }                       
console.log(mainobj);

Please share this code snnipt

Post Created 89

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top