0

I am trying to transform the array using .reduce function into an object of type:

{
"date": xxxx,
"amount": xxxx,
}

Insted of:

{01-2018: 0, 09-2019: 0, 02-2020: 0, 03-2020: 142.95999999999998, 04-2020: 652.78, …}

I would like to obtain a json like this one:

{
"date": 01-2018,
"amount": 0,
},
{
"date": 09-2019,
"amount": 0,
},
{
"date": 02-2020,
"amount": 0,
},
{
"date": 03-2020,
"amount": 142.95999999999998,
},

This is my code:

import React, { Component } from 'react';
import moment from "moment";


class RentabilidadMensual extends Component {

    //Este componente extendido es hijo de rentalidad y recibe de willmount los datos de la API.
    //Se calcculará la rentablidad mensual obtenida.

    constructor (props){
        super(props)


          this.state ={
            monthlyData: [],
            sortedData: [],
            monthlyTotalImport: [],
        }
    }


    componentWillMount() {

        var resultn = { NuevosDatosFinalizadas2: [] };
        var resultFinal = { NuevosDatosFinal: [] };


        function RentOperFinalizada (importe, Crypto, ValCryptoOperVenta , comisionCompra, comisionVenta, FechaCompra, FechaVenta){

            function NuevoImporte(){
              let tmpN = Number(ValCryptoOperVenta) * Number(Crypto)
              let R1 =  tmpN - (Number(comisionCompra) + Number(comisionVenta))
              return R1
            }

            let P = ((NuevoImporte() * 100)/Number(importe)) - 100 //Rentabilidad
            //console.log("voy a calcular: ",NuevoImporte() , " ", Number(importe) )
            let G = NuevoImporte() - Number(importe) //Cantidad ganada Neto
            let C = Number(comisionCompra) + Number(comisionVenta) //Comisiones Totales
            let D = moment(FechaVenta).diff( FechaCompra, 'days')
            console.log("Comisiones totales:", C)

            //R es Importe Total ganado en EUR
            //P es Rentabilidad en porcentaje
            //G es la cantidad ganada NETO en EUR.
            //C es Comisiones Totales en EUR
            //D es los días transcurrido entre la fecha compra y fecha venta.
            return [P.toFixed(2), G.toFixed(2), C.toFixed(2), D];
          }



        //Añadimos simplemente los campos:
        var DataFromApiTemp =  this.props.data.map((x) => Object.assign(
            x,
            x,
            {'RentabilidadPorcentaje' : ""}, {'CantidadGanadaNETO' : ""}, {'ComisionesTotales' : ""}, {'DiasTranscurrido' : ""}
            ));
            console.log("nuevo:",DataFromApiTemp )


            DataFromApiTemp.forEach((value, key) => {



            if (value.estado == "Finalizado" && value.moneda_crypto == this.props.selectedcrypto){

                    console.log("valor.estado: ", value.estado, key)

                    //Se llama la función para realizar los calculos.
                    var r = RentOperFinalizada (
                        value.compra_en_fiat, 
                        value.compra_crypto_cantidad, 
                        value.venta_crypto_valorOper , 
                        value.compra_gasto, 
                        value.venta_gasto, 
                        value.compra_fecha_oper, 
                        value.venta_fecha_oper)
                        //FIN

                        //Actualizamos los valores de estos campos:
                        value.RentabilidadPorcentaje = r[0];
                        value.CantidadGanadaNETO = r[1];
                        value.ComisionesTotales = r[2];
                        value.DiasTranscurrido = r[3];


                        resultn.NuevosDatosFinalizadas2.push(r);

                        console.log("Datos de la API", resultn.NuevosDatosFinalizadas2)

            }


        })

        console.log("dar algo modificado:", DataFromApiTemp)


        //Ya tenemos un nuevo Jason contruido donde se añadio previemnte las keys:
        //RentabilidadPorcentaje, CantidadGanadaNETO, ComisionesTotales, DiasTranscurrido
        let data = [];
        DataFromApiTemp.map(value => {
            let d = new Date(value.compra_fecha_oper);
            data.push({...value, date2: d});
        });


        //Ordenamos por fecha y creciente.
        let sortedData = data.sort((a, b) => a.date2 - b.date2);
        console.log(sortedData, 'sorted');


        //Añadimos nuevo campo yearMonth: "MES-FECHA"
        let monthlyData = [];
        sortedData.map(value => {
            let d = new Date(value.compra_fecha_oper);
            let yearMonth = ("0" + (d.getMonth() + 1)).slice(-2) + "-" + d.getFullYear();
            monthlyData.push({...value, yearMonth: yearMonth});
        });
        console.log(monthlyData, 'monthly data');




        let result = monthlyData.reduce((acc, item) => ({

            ...acc,
            [item.yearMonth]: (acc[item.yearMonth] || 0) + Number(item.CantidadGanadaNETO)
        }), {});
        this.setState([{monthlyTotalImport: result}]);

        console.log("Result:",  result)

//        var jsonObjFinal = 
//        { 
//          "fecha": monthlyTotalImport,
//          "importe": result,
//        }
//
//        resultFinal.NuevosDatosFinal.push(jsonObjFinal);
//



    }

    render() {

        const DataFromApi = this.props.data;

        var TipoCrypto = this.props.selectedcrypto;


        return (
            <div>
                test...
                {JSON.stringify(this.state.monthlyTotalImport)}


            </div>
        );
    }
}

export default RentabilidadMensual;

Thank you.

DataFromApiTemp have this value (example):

enter image description here

Code tried from @canberker the result is:

enter image description here

My objective is to get a json format with my already data of month-yer and amount.

If I have this data I will be able to charts this ressult.....

1
  • Input data is not an array but an object. Also 01-2018 as object key is wrong. Is it quoted like this "01-2018" Commented May 9, 2020 at 7:52

3 Answers 3

1
let aggregatedMonthlyData = monthlyData.reduce((acc, item) => ({
    ...acc,
    [item.yearMonth]: (acc[item.yearMonth] || 0) + Number(item.CantidadGanadaNETO)
    }), {}
);

const formatAggregatedMonthlyData = function(aggregatedMonthlyData) {
    const dateFieldName = 'date';
    const amountFieldName = 'amount';

    const result = [];
    for (let [date, amount] of Object.entries(aggregatedMonthlyData)) {
        result.push({
            [dateFieldName]: date,
            [amountFieldName]: amount
        });
    }
    return result;
}

const result = formatAggregatedMonthlyData(aggregatedMonthlyData);
console.log(result);
Sign up to request clarification or add additional context in comments.

8 Comments

This code is returning a nest json with a lot of key, value. I will put a image to show you.
@Kentron.dna Could you share a single instance from your input data? I believe the data is not in the format that you presented in the question.
Yes I will share I trying this code and the result is very strange.
@Kentron.dna When I look at the result, I get the impression that your monthlyData is formed by objects in an array. That's why "date" gets the value 0 as this is the array index. And "amount" gets the entire object found in the array index 0. So I am guessing your monthly data looks like: [{CantidadGanadaNETO: "", .... }, {CantidadGanadaNETO: "", .... }] Is this correct?
@Kentron.dna I will take a look. Can you try the latest code with "map" that I just added as an edit.
|
0

Assuming your input value is like below

const input = {"01-2018": 0, "09-2019": 0, "02-2020": 0, "03-2020":
142.95999999999998, ..}

Below solution will work

const transformData = input => 
   JSON.stringify(Object.entries(input).reduce((acc, [date, amount]) => [...acc, { date, amount }], []))


const output = transformData(input)

3 Comments

Where do you put these line?
@Kentron.dna here we have created a function called 'transformData' and then calling this method by passing the input value.
@Kentron.dna there was a minor mistake, updated the answer.
0

Using the Reduce method, we can get the expected result Array of objects

Try this:

let monthlyData=[{
 "year":"2019",
  "price":19
},{
 "year":"2020",
  "price":20
}]

let output = monthlyData.reduce((acc,current)=>{
    let item={
        "date":current.year,
        "amount":current.price
    }
    acc.push(item)
    return acc
  },
  []
)

console.log(output)

Comments

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.