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:[]});