2 problems, add elements in array conditionally and then create another array with values per date

Asked

Viewed 51 times

0

FROM THE RESULT OF THE CALL TO THE API I HAVE THAT RESULT:

var origemApi = [
      { data: '23-04', tipo: 'ADP' },
      { data: '23-04', tipo: 'IDP' },
      { data: '23-04', tipo: 'ADP' },
      { data: '23-05', tipo: 'ADP' },
      { data: '23-05', tipo: 'IDP' },
      { data: '23-05', tipo: 'ADP' },
      { data: '23-06', tipo: 'IDP' },
      { data: '23-06', tipo: 'ADP' },
      { data: '23-06', tipo: 'IDP' },
      { data: '23-06', tipo: 'IDP' },
      { data: '23-06', tipo: 'ADP' }
    ]

No Vue using groupBy and having achieved this result:

var destinoFront = [
      { data: '23-04', tipo: 'ADP', total: '10' },
      { data: '23-04', tipo: 'IDP', total: '20' },
      { data: '23-05', tipo: 'ADP', total: '30' },
      { data: '23-06', tipo: 'ADP', total: '15' },
      { data: '23-06', tipo: 'IDP', total: '15' }
    ]

that is, I grouped by type and date and calling SUM managed to generate the total It turns out that in index 3 (date 23-04), I only have type ADP of total 30 of a total of 30 records therefore, I DO NOT HAVE other index idp with total 0 type:

var destinoFront = [
      { data: '23-04', tipo: 'ADP', total: '10' },
      { data: '23-04', tipo: 'IDP', total: '20' },
      { data: '23-05', tipo: 'ADP', total: '30' },
      { data: '23-05', tipo: 'IDP', total: '0' },
      { data: '23-06', tipo: 'ADP', total: '15' },
      { data: '23-06', tipo: 'IDP', total: '15' }
    ]

need perhaps with splice push an index that is missing because I need to ALWAYS on the same date have 2 TYPES (ADP and IDP) to power some graphics that need a date where I have two data for the x and y axes.

It happens that in the API of a total of 30 records, if all are ADP(or vice versa) the IDP as it does not exist at that date, the query does not return me IDP 0. That’s the part of the problem...

Supposing I got this, now you need to count the two guys in a single kind:

var primeiroProblemaResolvido = [
      { data: '23-04', tipo: 'ADP', total: '10' },
      { data: '23-04', tipo: 'IDP', total: '20' },
      { data: '23-05', tipo: 'ADP', total: '30' },
      { data: '23-05', tipo: 'IDP', total: '0' },
      { data: '23-06', tipo: 'ADP', total: '15' },
      { data: '23-06', tipo: 'IDP', total: '15' }
    ]

var resultadoFinalEsperado = [
    ['23-04', '10', '20']
    ['23-05', '30', '0']
    ['23-06', '15', '15'] // ou seja, um aaray com a data, o tipo ADP e o tipo IDP(apenas os valores
]

Already try everything using map and filter with push and splice but when I solve the first problem, the second gets lost and vice versa.

  • I cannot format this text correctly...

1 answer

0


--Edit--

I made a revision of the code, because although it is working previously, I fear that in some extreme case, known as "edge case", the code may produce an unexpected result, since objects do not guarantee that they will be iterated in the same order as their properties have been added.

Instances of Map on the other hand guarantee this order, so I used a map to make the union of objects with the same date easier.

var problema = [
    { data: '23-04', tipo: 'ADP', total: '10' },
    { data: '23-04', tipo: 'IDP', total: '20' },
    { data: '23-05', tipo: 'ADP', total: '30' },
    { data: '23-06', tipo: 'ADP', total: '15' },
    { data: '23-06', tipo: 'IDP', total: '15' }
]

var solucao = Array.from(problema.reduce((acc, d) => acc.set(d.data, [
    d.data,
    d.tipo === 'ADP' ? d.total : (acc.has(d.data) ? acc.get(d.data)[1] : '0'),
    d.tipo === 'IDP' ? d.total : (acc.has(d.data) ? acc.get(d.data)[2] : '0')
]), new Map()).values());

console.log(solucao);

  • user 140828 !!!!!!!! my geniooo, you are from the earth itself? kkkk your code fit perfectly!!!! I used your second solution, I found it more elegant with filter and map, I just didn’t understand the % in filter.... but look, you just solved the second problem, I think you forgot the first problem or I couldn’t explain myself properly. The first problem ta just above where sometimes a TYPE does not come from the API for example IDP has filled (with 30) the ADP does not come and I need it with 0 so I tried with splice unsuccessfully. looking from the beginning of the text look at the second array.

  • @sidiara, I did a review of the code.

  • My dear..... God bless you and may He take care of your reasoning. Whoever thinks with precision and logic has a source of riches. My thank you! Saved my weekend and 3 months studying ES

Browser other questions tagged

You are not signed in. Login or sign up in order to post.