I am using reduce function and some reason I get an error on the console:
(index):77 Uncaught TypeError: filtered.push is not a function
DispatchedNumber
should exist before mapping into Lines
array.
function payload(order) {
return {
OrderId: order.ORDERID,
Lines: order.ITEMS.reduce(function(filtered, item) {
if (item.DispatchedNumber != undefined) {
var newItem = {
Qty: item.Qty,
QtyShipped: item.DispatchedNumber
}
filtered.push(newItem);
}
return filtered;
})
}
}
test = payload({
"ORDERID": 1233,
"ITEMS": [
{ Qty: 123, DispatchedNumber: 111 },
{ Qty: 111 },
{ Qty: 444, DispatchedNumber: 555 },
{ Qty: 443, DispatchedNumber: 323 }
]
})
Usage:
console.log(JSON.stringify(test, null, 2));
Example: https://jsfiddle.net/j6097w9d/2/