Combining JavaScript objects in an array with same key value

后端 未结 4 867
太阳男子
太阳男子 2020-12-19 18:21

I have been trying to combine objects inside an array with same id-value. The array that I have is like this:

[
{\"id\" : \"abcd\",\"val1\" : 1,\"val2\": 1,          


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 18:53

    Refer the below example

    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('
    ' + JSON.stringify(arrOut, 0, 4) + '
    ');

提交回复
热议问题