JavaScript.push () inserts variable instead of variable value

0

I'm having trouble with this code:

Expected Exit:

detalhes:[{
    01: 4570.00,
    02: 4301.68,
    03: 650.00,
    12: 700.00,
}]

reality:

0:[{
  {
    mes: 4570.00,
  },
  {
    mes: 4301.68,
  },
  {
    mes: 650.00,
  },
  {
    mes: 700.00,
  }
}]

I did not understand why it does not print the value of the variable mes

var datas = {
        "venda_detalhes": {
            "2018-01-10 00:00:00": "4570.00",
            "2018-02-15 00:00:00": "4301.68",
            "2018-03-17 00:00:00": "650.00",
            "2017-12-22 14:21:31": "700.00"
        }
      };
detalhes = [];
for (let prop in datas.venda_detalhes) {
    let data = new Date(prop);
    let mes = (data.getMonth() +1);
    let value = datas.venda_detalhes[prop];

    detalhes.push({mes : value});                        
}

console.log(detalhes);
    
asked by anonymous 19.03.2018 / 23:01

2 answers

1

Whenever you are building an object you have to define key and value pairs. Where the key is actually a name, the name of the field to which you want to associate the value.

let pessoa1 = {
    nome: "Marcos",
    idade: 23
};

Notice that name and age are actually strings , object properties, not values that come from variables.

In your case if you want to construct the name of a field with values that come from variables you can do it using the indexing operator for example:

let var1 = "desportivo";
pessoa1[var1] = "Ferrari";

Applying this to your example:

var datas = {
        "venda_detalhes": {
            "2018-01-10 00:00:00": "4570.00",
            "2018-02-15 00:00:00": "4301.68",
            "2018-03-17 00:00:00": "650.00",
            "2017-12-22 14:21:31": "700.00"
        }
      };
      
detalhes = [];
datasMes = {}; //objeto para acumular todas as datas

for (let prop in datas.venda_detalhes) {
    let data = new Date(prop);
    let mes = (data.getMonth() +1);
    let value = datas.venda_detalhes[prop];
    datasMes[mes] = value; //mais uma propriedade no objeto
}

detalhes.push(datasMes); //juntar o objeto ao array

console.log(detalhes);
    
19.03.2018 / 23:20
0

Interpreting the code:

1 - The 'details' array is receiving an object at each iteration

2 - The object is being built with a property of name 'mes' with a value 'number'

I found this practical output but would edit the object in detail to include 'value'; would generate an output like this

0:[{
  {
    mes: 1
    value: 4570.00,
  },
  {
    mes: 2
    value: 4301.68,
  },
  {
    mes: 3
    value: 650.00,
  },
  {
    mes: 12
    value: 700.00,
  }
}]

This allows a new iteration without needing the for ... in ... within the properties.

I have this code that generates the desired output.

var datas = {
        "venda_detalhes": {
            "2018-01-10 00:00:00": "4570.00",
            "2018-02-15 00:00:00": "4301.68",
            "2018-03-17 00:00:00": "650.00",
            "2017-12-22 14:21:31": "700.00"
        }
      };
detalhes = [];
detalhesDoMeses = {};
for (let prop in datas.venda_detalhes) {
    let data = new Date(prop);
    let mes = (data.getMonth() +1);
    let value = datas.venda_detalhes[prop];
    
    detalhesDoMeses[mes] = value;                         
}

detalhes.push(detalhesDoMeses);

console.log(detalhes);

The difference between the two is that in the first the array receives objects (not expected), in the second an object only.

Then look for something about objects vs arrays in javascript, they are very similar structures.

    
19.03.2018 / 23:37