Multidimensional javascript array with angular is undefined

-2

I'm trying to create a multidimensional array but it returns me results [array.status] 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);
    });
    
asked by anonymous 21.10.2016 / 18:23

1 answer

1

First, this is not an array of array type (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 the result you are looking for is a property for each state and each property of that will have an array with the sales, then:

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);
    })
}
    
24.10.2016 / 19:18