0

I am trying to access property values from an array of objects. I can access PurchaseOrder and DocumentCurrency property but not able to access NetPriceAmount

NetPriceAmount property is part of to_PurchaseOrderItem object.

Here is the link to js fiddle which has complete JSON result https://jsfiddle.net/d3b9cLas/

This is a layout of JSON result;

enter image description here

This is what have tried so far and I am not able to read NetPriceAmount value.

pos.forEach(({
        PurchaseOrder: PurchaseOrder,
        DocumentCurrency: Currency,
        NetPriceAmount: NetPriceAmount
     }) => {
        console.log(NetPriceAmount)
    });
greybeard
  • 2,249
  • 8
  • 30
  • 66
user1263981
  • 2,953
  • 8
  • 57
  • 98

1 Answers1

1

You have to take into account that some properties might not always exist. In an example you linked to, the first object does not have a to_PurchaseOrderItem property.

Here is how it could work:

const getPrices = (data) =>
    data.flatMap((obj) =>
        obj.to_PurchaseOrderItem?.results?.map(res => ({
            order: obj.PurchaseOrder,
            price: res.NetPriceAmount,
            currency: obj.DocumentCurrency
        }))
    ).filter(Boolean);

// Example (removed irrelevant properties)
const pos =  [{
    "DocumentCurrency": "", 
    "PurchaseOrder": "4500000000"
  }, {
    "DocumentCurrency": "GBP",
    "to_PurchaseOrderItem": { "results": [{ "NetPriceAmount": "50.00" }] },
    "PurchaseOrder": "4500000001",
  }, {
    "DocumentCurrency": "GBP",
    "to_PurchaseOrderItem": { "results": [{"NetPriceAmount": "13.00"}] },
    "PurchaseOrder": "4500000002",
  }, {
    "DocumentCurrency": "GBP",
    "to_PurchaseOrderItem": { "results": [{ "NetPriceAmount": "14000.00" }] },
    "PurchaseOrder": "4500000003",
  }];
  
console.log(getPrices(pos));

To sum amounts in the same order, and include also the orders that have no prices at all (defaulting to 0):

const getPrices = (data) =>
    data.map((obj) =>
        (obj.to_PurchaseOrderItem?.results ?? []).reduce((acc, res) => {
            acc.price += +res.NetPriceAmount;
            return acc;
        }, {
            order: obj.PurchaseOrder,
            price: 0,
            currency: obj.DocumentCurrency
        })
    );

// Example extended with multiple prices per order
const pos =  [{
    "DocumentCurrency": "", 
    "PurchaseOrder": "4500000000"
  }, {
    "DocumentCurrency": "GBP",
    "to_PurchaseOrderItem": { "results": [{ "NetPriceAmount": "50.00" }, { "NetPriceAmount": "25.00" }] },
    "PurchaseOrder": "4500000001",
  }, {
    "DocumentCurrency": "GBP",
    "to_PurchaseOrderItem": { "results": [{"NetPriceAmount": "13.00"}, { "NetPriceAmount": "9.50" }] },
    "PurchaseOrder": "4500000002",
  }, {
    "DocumentCurrency": "GBP",
    "to_PurchaseOrderItem": { "results": [{ "NetPriceAmount": "14000.00" }] },
    "PurchaseOrder": "4500000003",
  }];
  
console.log(getPrices(pos));
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Brilliant! Is it possible to loop through to_PurchaseOrderItem and get the total NetPriceAmount of that one order. One order can have multiple items. to_PurchaseOrderItem -> results -> [0]---NetPriceAmount, [1]---NetPriceAmount, [2]---NetPriceAmount – user1263981 Jun 11 '23 at 12:56
  • Sure that is possible. Have a go at it. If you cannot make it work, have a look at group and sum questions on this site, like [this one](https://stackoverflow.com/questions/29364262/how-to-group-by-and-sum-an-array-of-objects), and it you still cannot make it work, ask a new question about the problem you encountered. – trincot Jun 11 '23 at 12:59
  • i am going to try that out. First order 4500000000 still needs to be retrieved even if it doesn't have any line item. – user1263981 Jun 11 '23 at 13:02
  • OK, I added a second snippet to my answer. – trincot Jun 11 '23 at 13:07