Javascript.push() inserts variable instead of variable value

Asked

Viewed 278 times

0

I’m having trouble with that 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 didn’t understand why he doesn’t 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);

2 answers

1


Whenever you’re 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 what name and age they really are strings, object properties and not values that come from variables.

In your case if you want to build the name of a field with values that comes 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);

0

Interpreting the code:

1 - The details array is receiving an Object every iteration

2 - The Object is being built with a property named 'mes' with a value 'number'

I found this practical output but would edit Object in detail to include 'value'; it 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 the need 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 takes Objects (not expected), in the second an Object only.

Then look for something on Javascript Objects vs arrays, they are very similar structures.

Browser other questions tagged

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