Array is not being created correctly

Asked

Viewed 68 times

1

I have a variable from AngularJS which saves any json I upload, the json data is similar to that:

{
    "Products": "Pimenta Vermelha muito boa",
    "Product_Code": "XXXXX",
    "RTV": "Alisson Acioli",
    "Regional": "Sudeste",
    "Cliente": "AGRO",
    "Estado": "MG",
    "Pricing_Zone": "XXX",
    "Quinzena": "Q2",
    "New_Product_Description": "Old Product",
    "Active_Ingredient": "Pimenta Vermelha",
    "Familia": "Pimenta",
    "Group": "Grupo do IA",
    "Linha": "Albaugh",
    "Devolução_Faturamento": "Faturamento",
    "Dia": 10,
    "mes": 1,
    "ano": 2015,
    "Concatenar": "",
    "Crop": "22 - CROP",
    "Version": "OPQ",
    "Tipo_Nota": "",
    "Data_Lancamento": "",
    "Data_Vencimento": "",
    "Prazo": "",
    "Condicao_Pagamento": "",
    "IGM": 1.0,
    "COGS": 2.0,
    "Net_Sales_VP": 1.000,
    "Net_Sales": 500,
    "Volume": 2,
    "ICMS_Tax": 100,
    "Freight": 100,
    "Interest": "-",
    "Receita_Invoice": 3.000,
    "Preco_Lista_Invoice_a_vista": "3,9",
    "Preco_a_prazo": "3,9",
    "Faturamento_Lista_Invoice": 10,
    "Net_Sales_Lista": 10,
    "Discount": "-2%",
    "Range_Capaing": "#N/D",
    "Range_Capaing_2": "#N/D",
    "Preco_Net_Lista_usd": "3,6",
    "IGM_Lista": 2.957,
    "Cambio_Dia": "2,571",
    "Faturamento_rs": "R$ 1.500",
    "Cambio_mes": "",
    "Faturamento_usd_Financeiro": ""
  }

I filter this data and insert it into a variable that is an array. So far so good, everything working.

What I have to do is create an array with some data from that json, like this:

/* Variáveis para gerenciar o filtro */
        var GraphOne = [];
        var GraphTwo = [];

        /* Variáveis para inserir no Highcharts */
        var HighchartsOne = [];
        var HighchartsTwo = [];

        $scope.filteredDados.forEach(function(arrayItem){

            if(arrayItem.Condicao_Pagamento != ''){
                GraphOne[arrayItem.Condicao_Pagamento] = GraphOne[arrayItem.Condicao_Pagamento] || [];
                GraphOne[arrayItem.Condicao_Pagamento].push(arrayItem.Net_Sales);
            }

            if(arrayItem.RTV != ''){
                GraphTwo[arrayItem.RTV] = GraphTwo[arrayItem.RTV] || [];
                GraphTwo[arrayItem.RTV].push({netsales:arrayItem.Net_Sales, igm: arrayItem.IGM, familia: arrayItem.Familia});
            }
        });

In the first block it creates an array and inserts payment conditions:

if(arrayItem.Condicao_Pagamento != ''){
                    GraphOne[arrayItem.Condicao_Pagamento] = GraphOne[arrayItem.Condicao_Pagamento] || [];
                    GraphOne[arrayItem.Condicao_Pagamento].push(arrayItem.Net_Sales);
                }

He inserts it right. I make one console.log(Graphone); and it returns me the right array. But in the second block that’s the problem. I put the console.log(GraphTwo); and he apparently on the console gives me the result:

Array[ ]

And after a few seconds I click on this Array[ ] it informs me the data that has in it, which in case is what I need. The strange thing is that when it appears this Array[ ] and I click on it immediately nothing happens, but after a few seconds, if I click again, it displays me. The bad thing is that I need to read this Array later in the code, but I think the code thinks it is an empty array and does not read. The first block normally reads.

I don’t know if it’s because his Intel (Graphtwo[arrayItem.RTV]) is a name that contains type spaces Alisson Acioli, but I really don’t know what it can be.

  • You’re using the wrong data structure, aren’t you? You don’t want an array, you want an object

  • Show what would be the goal of the object that you are generating and also the part that it makes the GraphTwo

  • There are many points that need attention here @Alisson Acioli. The Example you posted will never enter the condition because the Payment Condition is empty. Second I think here GraphTwo[arrayItem.RTV] = GraphTwo[arrayItem.RTV] || []; is wrong. At the bottom line he tries to access GraphTwo[arrayItem.RTV].push but.RTV arrayItem may not exist. Third, if you click again to display the array, it is either sync problem or bug. Room, I’ll be happy to help with a verifiable example, maybe on Jsfiddle.

No answers

Browser other questions tagged

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