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!!

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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)
what is storeStream()? it's coming from a 3rd party library?
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);

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.