1

I am a little confused in how to proceed with this scenario using ramda. Here is the JSON that I am working with.

{ "type" : "CartWsDTO",
"Cartentries" : [ { "entryNumber" : 1, "flightGroup" : { "PAXDetails" : [ { "paxID" : "1", "paxPrice" : "770.82", "paxType" : "ADT" }, { "paxID" : "2", "paxPrice" : "770.82", "paxType" : "ADT" } ] } }, { "entryNumber" : 2, "flightGroup" : { "PAXDetails" : [ { "paxID" : "1", "paxName" : "Vinitha", "paxPrice" : "770.82", "paxSurname" : "Vinitha", "paxType" : "ADT" }, { "paxID" : "2", "paxName" : "Prahal", "paxPrice" : "770.82", "paxSurname" : "Prahal", "paxType" : "ADT" } ] } } ] }

There are 2 CartEnteries in the above JSON. There is an array named paxDetails in flightGroup of each entry. From this paxDetails array I want to pick the paxPrice and make a sum of all the pax prices for that cart entry. In traditional for loop and if conditions I am able to achieve it. But using Ramda I couldn't understand how to start with. Kindly provide me a solution.

Thanks in advance.

1 Answer 1

1

It's not entirely clear to me what you're looking for as output. Here's a solution that simply returns the sum of the two sets of prices and returns an array with those values:

var calcTotals = R.pipe(
    R.prop('Cartentries'),
    R.map(R.pipe(
        R.path(['flightGroup', 'PAXDetails']),
        R.map(R.pipe(R.prop('paxPrice'), Number)),
        R.sum
    ))
);
calcTotals(cart); //=> [1541.64, 1541.64]

But if you wanted a different sort of output, such as

{1: 1541.64, 2: 1541.64}

or

[{entryNumber : 1, total: 1541.64}, {entryNumber: 2. total: 1541.64}]

or whatever, you'd have to make some changes.

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

1 Comment

Thanks a lot.. This is what exactly I wanted.. This saved my time. I think I should get deeper understanding of the API to perform these things.

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.