Polo Thanks for posting your question in Microsoft Q&A. Yes, you are right. You can simply achieve this with Inline Code action and the following code snippet was generated using AI tool/API service.
const array1 = [
{
"lineNum": 1,
"qty": 10
},
{
"lineNum": 1,
"qty": 20
},
{
"lineNum": 2,
"qty": 15
}
];
const sums = {};
for (const obj of array1) {
const lineNum = obj.lineNum;
const qty = obj.qty;
if (lineNum in sums) {
sums[lineNum] += qty;
} else {
sums[lineNum] = qty;
}
}
const array2 = [];
for (const [lineNum, qtySum] of Object.entries(sums)) {
const obj = { lineNum: parseInt(lineNum), qty: qtySum };
array2.push(obj);
}
return JSON.stringify(array2);
I tested this quickly in my logic app and seems to work fine with the below output.
Output:
Note: this is just sample code snippet, and you need to modify this for your scenario (like other elements) and validate for all production use cases.
I hope this helps and let me know if you have any questions.
If you found the answer to your question helpful, please take a moment to mark it as Yes
for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.