Reputation: 55
I need your support to generate the following result in ramda.
I have done some ways and I have reached these results but now what I need is to be done as a merge by the productCode generating the following result, where serviceType will be an array that is ordered and related by duration:
DATA
data = [
{
"id": "ck5ii4ysl04vn0744z7liu0f3",
"pdvIntegrationStatus": "WAITING",
"sapIntegrationStatus": "WAITING",
"updatedAt": "2020-01-17T18:34:13.075Z",
"category": {
"code": "CEL",
"description": "Celular e Smartphones"
},
"serviceType": {
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
},
"productCode": "4004593",
"duration": [
24,
12
]
},
{
"id": "ck5ii4yr704vc0744sjw3tljd",
"pdvIntegrationStatus": "WAITING",
"sapIntegrationStatus": "WAITING",
"updatedAt": "2020-01-17T18:34:13.075Z",
"category": {
"code": "CEL",
"description": "Celular e Smartphones"
},
"serviceType": {
"code": "GE",
"description": "descrição da garantia estendida"
},
"productCode": "4004593",
"duration": [
12
]
},
{
"id": "ck5ii4yr704vc0744sjw3tljr",
"pdvIntegrationStatus": "INTEGRATED",
"sapIntegrationStatus": "INTEGRATED",
"updatedAt": "2020-01-18T15:34:09.000Z",
"category": {
"code": "DUC",
"description": "Duchas e chuveiros"
},
"serviceType": {
"code": "GE",
"description": "descrição da garantia estendida"
},
"productCode": "4004594",
"duration": [
24
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f6",
"pdvIntegrationStatus": "WAITING",
"sapIntegrationStatus": "INTEGRATED",
"updatedAt": "2020-01-17T18:34:13.075Z",
"category": {
"code": "DUC",
"description": "Duchas e chuveiros"
},
"serviceType": {
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
},
"productCode": "4004596",
"duration": [
24,
12
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f0",
"pdvIntegrationStatus": "INTEGRATED",
"sapIntegrationStatus": "WAITING",
"updatedAt": "2020-01-17T18:34:13.075Z",
"category": {
"code": "CEL",
"description": "Celular e Smartphones"
},
"serviceType": {
"code": "GE",
"description": "descrição da garantia estendida"
},
"productCode": "4004595",
"duration": [
12
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f1",
"pdvIntegrationStatus": "INTEGRATED",
"sapIntegrationStatus": "INTEGRATED",
"updatedAt": "2020-01-17T18:34:13.075Z",
"category": {
"code": "DUC",
"description": "Duchas e chuveiros"
},
"serviceType": {
"code": "GE",
"description": "descrição da garantia estendida"
},
"productCode": "4004596",
"duration": [
12
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f2",
"pdvIntegrationStatus": "INTEGRATED",
"sapIntegrationStatus": "INTEGRATED",
"updatedAt": "2020-01-17T18:34:13.075Z",
"category": {
"code": "VENT",
"description": "Ventiladores"
},
"serviceType": {
"code": "GE",
"description": "descrição da garantia estendida"
},
"productCode": "4004597",
"duration": [
24
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f4",
"pdvIntegrationStatus": "INTEGRATED",
"sapIntegrationStatus": "INTEGRATED",
"updatedAt": "2020-01-17T18:34:13.075Z",
"category": {
"code": "DUC",
"description": "Duchas e chuveiros"
},
"serviceType": {
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
},
"productCode": "4004594",
"duration": [
12
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f5",
"pdvIntegrationStatus": "WAITING",
"sapIntegrationStatus": "WAITING",
"updatedAt": "2020-01-17T18:34:13.075Z",
"category": {
"code": "CEL",
"description": "Celular e Smartphones"
},
"serviceType": {
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
},
"productCode": "4004595",
"duration": [
24
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f9",
"pdvIntegrationStatus": "WAITING",
"sapIntegrationStatus": "WAITING",
"updatedAt": "2020-01-17T15:34:13.000Z",
"category": {
"code": "VENT",
"description": "Ventiladores"
},
"serviceType": {
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
},
"productCode": "4004597",
"duration": [
24
]
}
]
RESULT EXPECTED
resultExpected = [
{
"id": "ck5ii4ysl04vn0744z7liu0f3",
"productCode": "4004593",
"category": {
"code": "CEL",
"description": "Celular e Smartphones"
},
"updatedAt": "2020-01-17T18:34:13.075Z",
"pdvIntegrationStatus": false,
"sapIntegrationStatus": false,
"serviceType": [
{
"code": "GE",
"description": "descrição da garantia estendida"
},{
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
}
],
"duration_GE": [
12
],
"duration_RF": [
24,
12
]
},
{
"id": "ck5ii4yr704vc0744sjw3tljr",
"productCode": "4004594",
"category": {
"code": "DUC",
"description": "Duchas e chuveiros"
},
"updatedAt": "2020-01-18T15:34:09.000Z",
"pdvIntegrationStatus": true,
"sapIntegrationStatus": true,
"serviceType": [
{
"code": "GE",
"description": "descrição da garantia estendida"
},
{
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
}
],
"duration_GE": [
12
],
"duration_RF": [
24
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f0",
"productCode": "4004595",
"category": {
"code": "CEL",
"description": "Celular e Smartphones"
},
"updatedAt": "2020-01-17T18:34:13.075Z",
"pdvIntegrationStatus": true,
"sapIntegrationStatus": false,
"serviceType": [
{
"code": "GE",
"description": "descrição da garantia estendida"
},
{
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
}
],
"duration_GE": [
24
],
"duration_RF": [
12
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f6",
"productCode": "4004596",
"category": {
"code": "DUC",
"description": "Duchas e chuveiros"
},
"updatedAt": "2020-01-17T18:34:13.075Z",
"pdvIntegrationStatus": false,
"sapIntegrationStatus": true,
"serviceType": [
{
"code": "GE",
"description": "descrição da garantia estendida"
}, {
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
}
],
"duration_GE": [
12
],
"duration_RF": [
24,
12
]
},
{
"id": "ck5ii4ysl04vn0744z7liu0f2",
"productCode": "4004597",
"category": {
"code": "VENT",
"description": "Ventiladores"
},
"updatedAt": "2020-01-17T18:34:13.075Z",
"pdvIntegrationStatus": true,
"sapIntegrationStatus": true,
"serviceType": [
{
"code": "GE",
"description": "descrição da garantia estendida"
},
{
"code": "RF",
"description": "descrição SEGURO ROUBO OU FURTO"
}
],
"duration_GE": [
24
],
"duration_RF": [
24
]
}
]
I have tried this code to make but the duration array saves it randomly as the information is coming and I need the first arrangement within duration to be for "Garantia Extendida" and the second for "Robo o Furto" and know how to identify that duration arrangement to which it belongs, for example:
Duration: {GE: ['12', '24'], RF: ['24']}
> const groupByTransaction = R.groupBy(R.prop('productCode'));
> const mergeToObject = (rows) => Object.values(rows).map( item => ( { ...R.pickAll(['id'], item[0]), ...R.pickAll(['productCode'], item[0]), ...R.pickAll(['category'],
> item[0]), ...R.pickAll(['updatedAt'], item[0]),
> pdvIntegrationStatus: R.contains(R.pluck('pdvIntegrationStatus',
> item)[0], 'INTEGRATED'), sapIntegrationStatus:
> R.contains(R.pluck('sapIntegrationStatus', item)[0], 'INTEGRATED'),
> serviceType: R.pluck('serviceType', item), duration_GE:
> (R.pluck('duration', item))[1], duration_RF: (R.pluck('duration',
> item))[0] } ));
> const hasDuplicates = R.pipe( groupByTransaction, mergeToObject );
>
> rows = hasDuplicates(rows);
Upvotes: 0
Views: 37
Reputation: 55
I got this way:
const mergeToObject = (rows) => Object.values(rows).map(item => ( {
...R.pickAll(['id'], item[0]),
...R.pickAll(['productCode'], item[0]),
...R.pickAll(['category'], item[0]),
...R.pickAll(['updatedAt'], item[0]),
pdvIntegrationStatus: R.contains(R.pluck('pdvIntegrationStatus', item)[0], 'INTEGRATED') &&
R.contains(R.pluck('pdvIntegrationStatus', item)[1], 'INTEGRATED'),
sapIntegrationStatus: R.contains(R.pluck('sapIntegrationStatus', item)[0], 'INTEGRATED') &&
R.contains(R.pluck('sapIntegrationStatus', item)[1], 'INTEGRATED'),
serviceType_GE: R.contains((R.pluck('serviceType', item), 'GE'))?'descrição da garantia estendida':'',
serviceType_RF: R.contains((R.pluck('serviceType', item), 'RF'))?'descrição SEGURO ROUBO OU FURTO':'',
duration_GE: (item[0].serviceType.code === 'GE') ?
R.sort(sortByDuration, (R.pluck('duration', item))[0]) :
((R.pluck('duration', item))[1])?R.sort(sortByDuration, (R.pluck('duration', item))[1]):[] || [],
duration_RF: (item[0].serviceType.code === 'RF') ?
R.sort(sortByDuration, (R.pluck('duration', item))[0]) :
((R.pluck('duration', item))[1])?R.sort(sortByDuration, (R.pluck('duration', item))[1]):[] || []
} ));
const hasDuplicates = R.pipe( groupByTransaction, mergeToObject );
rows = hasDuplicates(rows);
It is obvious that its shape is much cleaner, kkkkkkk.... thanks
Upvotes: 0
Reputation: 50797
First off, a request: Before posting further questions, please read How to create a Minimal, Reproducible Example. There is far more data in the question than is necessary to demonstrate the requested behavior. There were more entries in the array than needed, and they had many more properties than were required. Scrolling back and forth between the input and the desired output to try to understand what you were doing was tedious enough that I almost gave up.
In my sample below, I reduce the number of properties, although I keep all the entries. Additional properties should make no difference to the technique.
So next time, please create a minimal example before asking. Thanks!
I'm not sure enough from your question, but I think this sample does what you're looking for:
const combineTypes = (
{serviceType: allTypes, ... base},
{id, serviceType, duration, ... rest}
) => ({
... rest,
serviceType: [... allTypes, serviceType],
... base,
[`duration_${serviceType.code}`]: duration
})
const transform = pipe (
groupBy (prop ('productCode')),
values,
map (reduce (combineTypes, {serviceType: []}))
)
const data = [{id: "ck5ii4ysl04vn0744z7liu0f3", serviceType: {code: "RF", description: "descrição SEGURO ROUBO OU FURTO"}, productCode: "4004593", duration: [24, 12]}, {id: "ck5ii4yr704vc0744sjw3tljd", serviceType: {code: "GE", description: "descrição da garantia estendida"}, productCode: "4004593", duration: [12]}, {id: "ck5ii4yr704vc0744sjw3tljr", serviceType: {code: "GE", description: "descrição da garantia estendida"}, productCode: "4004594", duration: [24]}, {id: "ck5ii4ysl04vn0744z7liu0f6", serviceType: {code: "RF", description: "descrição SEGURO ROUBO OU FURTO"}, productCode: "4004596", duration: [24, 12]}, {id: "ck5ii4ysl04vn0744z7liu0f0", serviceType: {code: "GE", description: "descrição da garantia estendida"}, productCode: "4004595", duration: [12]}, {id: "ck5ii4ysl04vn0744z7liu0f1", serviceType: {code: "GE", description: "descrição da garantia estendida"}, productCode: "4004596", duration: [12]}, {id: "ck5ii4ysl04vn0744z7liu0f2", serviceType: {code: "GE", description: "descrição da garantia estendida"}, productCode: "4004597", duration: [24]}, {id: "ck5ii4ysl04vn0744z7liu0f4", serviceType: {code: "RF", description: "descrição SEGURO ROUBO OU FURTO"}, productCode: "4004594", duration: [12]}, {id: "ck5ii4ysl04vn0744z7liu0f5", serviceType: {code: "RF", description: "descrição SEGURO ROUBO OU FURTO"}, productCode: "4004595", duration: [24]}, {id: "ck5ii4ysl04vn0744z7liu0f9", serviceType: {code: "RF", description: "descrição SEGURO ROUBO OU FURTO"}, productCode: "4004597", duration: [24]}];
console .log (
transform (data)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.min.js"></script>
<script>const {pipe, groupBy, prop, values, map, reduce} = R </script>
(Again note that not all the properties from your request are included in the output. If they're in the input, they should show up properly.)
We remove the id
s in this. The data has different ids for different entries, and there was no clear way to combine them. If we need to keep them all, we can do something similar to what we do with serviceType
.
The core is in the combineTypes
function, which is supplied to reduce
in the transform
pipeline. transform
is standard Ramda code, and I think should be fairly clear.
If this doesn't do what you're looking for, please add some clarification in the question or as a comment.
Upvotes: 2