0

I am trying to combine "scheduleDetails" Array for the same ID field within an OrderLines array

I tried using Groovy with which I got result, but one of the array is repeating which i am still troubleshooting. I want to explore if its easier with Javascript.

Input:

{
    "orderLines": [{
        "ID": "001",
        "orderedArticle" : {
            "scheduleDetails": [{
                "address": {
                    "street": [
                        "1234 Unknown blvd001-1"
                    ],
                    "city": "unknown001-1"
                }
            }]
        }
    },
    {
        "ID": "003",
        "orderedArticle" : {
            "scheduleDetails": [{
                "address": {
                    "street": [
                        "1234 Unknown blvd003-1"
                    ],
                    "city": "unknown003-1"
                }
            }]
        }
    },
    {
        "ID": "001",
        "orderedArticle" : {
            "scheduleDetails": [{
                "address": {
                    "street": [
                        "1234 Unknown blvd001-2"
                    ],
                    "city": "unknown001-2"
                }
            }]
        }
    },
    {
        "ID": "002",
        "orderedArticle" : {
            "scheduleDetails": [{
                "address": {
                    "street": [
                        "1234 Unknown blvd002-1"
                    ],
                    "city": "unknown002-1"
                }
            }]
        }
    },
    {
        "ID": "003",
        "orderedArticle" : {
            "scheduleDetails": [{
                "address": {
                    "street": [
                        "1234 Unknown blvd003-2"
                    ],
                    "city": "unknown003-2"
                }
            }]
        }
    }]
}

Output:

{
    "orderLines": [{
        "ID": "001",
        "orderedArticle": {
            "scheduleDetails": [{
                "address": {
                    "street": [
                        "1234 Unknown blvd001-1"
                    ],
                    "city": "unknown001-1"
                }
            },
            {
                "address": {
                    "street": [
                        "1234 Unknown blvd001-2"
                    ],
                    "city": "unknown001-2"
                }
            }]
        }
    },
    {
        "ID": "003",
        "orderedArticle": {
            "scheduleDetails": [{
                "address": {
                    "street": [
                        "1234 Unknown blvd003-1"
                    ],
                    "city": "unknown003-1"
                }
            },
            {
                "address": {
                    "street": [
                        "1234 Unknown blvd003-2"
                    ],
                    "city": "unknown003-2"
                }
            }]
        }
    },
    {
        "ID": "002",
        "orderedArticle": {
            "scheduleDetails": [{
                "address": {
                    "street": [
                        "1234 Unknown blvd002-1"
                    ],
                    "city": "unknown002-1"
                }
            }]
        }
    }]
}

Any help is much appreciated! TIA!!

biberman
  • 5,606
  • 4
  • 11
  • 35
tradersjoe
  • 37
  • 4

2 Answers2

0

You can use Array.prototype.reduce() following Object.values() to achieve this:

const oldOrderLinesObj = {
  "orderLines": [{
      "ID": "001",
      "orderedArticle" : {
          "scheduleDetails": [{
              "address": {
                  "street": [
                      "1234 Unknown blvd001-1"
                  ],
                  "city": "unknown001-1"
              }
          }]
      }
  },
  {
      "ID": "003",
      "orderedArticle" : {
          "scheduleDetails": [{
              "address": {
                  "street": [
                      "1234 Unknown blvd003-1"
                  ],
                  "city": "unknown003-1"
              }
          }]
      }
  },
  {
      "ID": "001",
      "orderedArticle" : {
          "scheduleDetails": [{
              "address": {
                  "street": [
                      "1234 Unknown blvd001-2"
                  ],
                  "city": "unknown001-2"
              }
          }]
      }
  },
  {
      "ID": "002",
      "orderedArticle" : {
          "scheduleDetails": [{
              "address": {
                  "street": [
                      "1234 Unknown blvd002-1"
                  ],
                  "city": "unknown002-1"
              }
          }]
      }
  },
  {
      "ID": "003",
      "orderedArticle" : {
          "scheduleDetails": [{
              "address": {
                  "street": [
                      "1234 Unknown blvd003-2"
                  ],
                  "city": "unknown003-2"
              }
          }]
      }
  }]
}

const newOrderLinesObj = {orderLines: Object.values(oldOrderLinesObj.orderLines.reduce((acc, curr) => {
  if(!acc[curr.ID]) {
    acc[curr.ID] = curr
  } else {
    acc[curr.ID].orderedArticle.scheduleDetails.push(...curr.orderedArticle.scheduleDetails)
  }

  return acc;
}, {}))}

console.log(newOrderLinesObj);

Here is a sandBox

zb22
  • 3,126
  • 3
  • 19
  • 34
  • Your code works, but All data is referenced from the dataContext object, which contains the following methods:getDataCount() - Gets the number of documents that are being processed. getStream(int) - Gets an InputStream for a given document index. getProperties(int) - Gets a Properties object for a given document index. storeStream(InputStream, Properties) - Stores the data located in the InputStream back to the process, along with any Properties.Data should be grabbed by the getStream(int) method, manipulated, then stored back to the dataContext using storeStream(InputStream, Properties) – tradersjoe Jun 22 '21 at 00:51
  • what is `storeStream()`? it's coming from a 3rd party library? – zb22 Jun 22 '21 at 19:12
0

You could use a set, and merge when existing ID is found again

e.g.

let input = {
  "orderLines": [{
      "ID": "001",
      "orderedArticle": {
        "scheduleDetails": [{
          "address": {
            "street": [
              "1234 Unknown blvd001-1"
            ],
            "city": "unknown001-1"
          }
        }]
      }
    },
    {
      "ID": "003",
      "orderedArticle": {
        "scheduleDetails": [{
          "address": {
            "street": [
              "1234 Unknown blvd003-1"
            ],
            "city": "unknown003-1"
          }
        }]
      }
    },
    {
      "ID": "001",
      "orderedArticle": {
        "scheduleDetails": [{
          "address": {
            "street": [
              "1234 Unknown blvd001-2"
            ],
            "city": "unknown001-2"
          }
        }]
      }
    },
    {
      "ID": "002",
      "orderedArticle": {
        "scheduleDetails": [{
          "address": {
            "street": [
              "1234 Unknown blvd002-1"
            ],
            "city": "unknown002-1"
          }
        }]
      }
    },
    {
      "ID": "003",
      "orderedArticle": {
        "scheduleDetails": [{
          "address": {
            "street": [
              "1234 Unknown blvd003-2"
            ],
            "city": "unknown003-2"
          }
        }]
      }
    }
  ]
};

let output = {
  "orderLines": []
};
let processed = new Set();

input.orderLines.forEach(inOrderLine => {
  if (!processed.has(inOrderLine.ID)) {
    processed.add(inOrderLine.ID);
    output.orderLines.push(inOrderLine);
  } else {
    let matched = output.orderLines.find(
      outOrderLine => outOrderLine.ID == inOrderLine.ID
    ).orderedArticle.scheduleDetails.push(
      inOrderLine.orderedArticle.scheduleDetails[0]
    );
  }
})

console.log(output);
Sharan Arumugam
  • 353
  • 6
  • 12
  • Your code works, but All data is referenced from the dataContext object, which contains the following methods:getDataCount() - Gets the number of documents that are being processed. getStream(int) - Gets an InputStream for a given document index. getProperties(int) - Gets a Properties object for a given document index. storeStream(InputStream, Properties) - Stores the data located in the InputStream back to the process, along with any Properties.Data should be grabbed by the getStream(int) method, manipulated, then stored back to the dataContext using storeStream(InputStream, Properties) – tradersjoe Jun 22 '21 at 00:34