Javascript multidimensional array with angular is undefined

Asked

Viewed 329 times

-2

I’m trying to create a multidimensional array but it returns to me results[array. state] is Undefined. I’m using angularjs:

results = new Array();

    var indice = 0;

    angular.forEach($scope.filteredDados, function(array, key){

        resultados[array.Estado][indice] = array.Venda;

        indice++;

    });

    angular.forEach(resultados, function (a,b){

        console.log(a+' '+b);
    });
  • What’s in this array $scope.filteredDados?

  • @Lenilsondecastro a json

  • I imagine so, but I meant what data is in it?

  • @Lenilsondecastro are like this: {"Ano":2016, "Mês": 1, "Estado":"SP", "Cultura": "x", "Segmento":"y", "Venda": "300.00"}

  • Then my reply should serve you as a look http://answall.com/a/160493/57237

1 answer

1


First, this is not an array type array (array[n][n]), what you are doing is an object that has an array in the properties (Object{x:[n]}). Ex:

var i = 0;
var result = null;
[{Estado: "SP", Venda: "200Temers"}].forEach((item) => { result[item.Estado][i] = ; i++; })
// Seu result vai virar isso aqui: 
// result = { SP: [ "200Temers" ] }

Okay, after this conceptual correction, let’s continue. I imagine that the result you are looking for is a property for each state and each property of that will have an array with sales, so:

var resultados = {};

$scope.filteredDados.forEach((arrayItem) => {
    resultados[arrayItem.Estado] = resultados[arrayItem.Estado] || [];
    resultados[arrayItem.Estado].push(arrayItem.Venda)
});

for (atributo in resultados) {
    resultados[atributo].forEach((venda) => {
        console.log(atributo + ": " + venda);
    })
}

Browser other questions tagged

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