var array = [{
"sequence" : 1,
"locationId" : "332228",
"lat" : 25.246511,
"lng" : 55.293837,
"stopName" : "332228",
"locationType" : "service",
"serviceType" : "Delivery",
"arrivalTime" : 37666,
"departureTime" : 37830,
"travelTime" : 1593,
"travelDistance" : 20985,
"travelCost" : 0,
"serviceTime" : 30,
"serviceTimeCost" : 0,
"tripNumber" : 0,
"orders" : [
{
"orderId" : "AQ137O1701240",
"SIZE1" : "28",
"SIZE2" : "520",
"SIZE3" : "52"
}
],
"stopId" : "SkhirG2ioZ"
},
{
"sequence" : 2,
"locationId" : "332228",
"lat" : 25.236407,
"lng" : 55.272403,
"stopName" : "332228",
"locationType" : "service",
"serviceType" : "Delivery",
"arrivalTime" : 38575,
"departureTime" : 38605,
"travelTime" : 1593,
"travelDistance" : 20985,
"travelCost" : 0,
"serviceTime" : 30,
"serviceTimeCost" : 0,
"tripNumber" : 0,
"orders" : [
{
"orderId" : "AQ137O1701233",
"SIZE1" : "23",
"SIZE2" : "402",
"SIZE3" : "30"
}
],
"stopId" : "H1iirfhisW"
},
{
"sequence" : 3,
"locationId" : "332228",
"lat" : 25.221368,
"lng" : 55.265915,
"stopName" : "332228",
"locationType" : "service",
"serviceType" : "Delivery",
"arrivalTime" : 39137,
"departureTime" : 39167,
"travelTime" : 974,
"travelDistance" : 5717,
"travelCost" : 0,
"serviceTime" : 30,
"serviceTimeCost" : 0,
"tripNumber" : 0,
"orders" : [
{
"orderId" : "AQ110O1705036",
"SIZE1" : "60",
"SIZE2" : "1046",
"SIZE3" : "68"
}
],
"stopId" : "H1csHM3jjb"
}]
var arrOut = [];
array.forEach(function(value) {
var existing = arrOut.filter(function(v, i) {
return v.locationId == value.locationId;
});
if (existing.length) {
var existingIndex = arrOut.indexOf(existing[0]);
arrOut[existingIndex].orders = arrOut[existingIndex].orders.concat(value.orders);
} else {
if(Array.isArray(value.orders)){
value.orders = value.orders
arrOut.push(value);
}
}
});
document.write('<pre>' + JSON.stringify(arrOut, 0, 4) + '</pre>');