[英]Get all values of a key in a nested array of objects using ramda.js
I am a little confused in how to proceed with this scenario using ramda. 我对如何使用ramda进行此方案感到有些困惑。 Here is the JSON that I am working with. 这是我正在使用的JSON。
{ "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. 上面的JSON中有2个CartEnteries。 There is an array named paxDetails in flightGroup of each entry. 每个条目的flightGroup中都有一个名为paxDetails的数组。 From this paxDetails array I want to pick the paxPrice and make a sum of all the pax prices for that cart entry. 从这个paxDetails数组中,我想选择paxPrice并对该购物车条目的所有pax价格求和。 In traditional for loop and if conditions I am able to achieve it. 在传统的for循环中,如果条件可以实现。 But using Ramda I couldn't understand how to start with. 但是使用Ramda我不知道如何开始。 Kindly provide me a solution. 请给我一个解决方案。
Thanks in advance. 提前致谢。
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. 或其他任何方式,您都必须进行一些更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.