简体   繁体   中英

Merge two arrays by value

I have a specific cuestion about merge arrays:

I'm using google charts and I need to do something like this

Combo Chart 在此处输入图片说明

To do something like that I need to fill this matrix 在此处输入图片说明

I did fine with axis x and axis y:

$scope.data= [];
            $scope.data[0]= ['Months'];

            angular.forEach($scope.consultors, function(consultor) {
                $scope.data[0].push(consultor.no_user);
            })

            angular.forEach(months, function(month) {
                $scope.data.push([month])
            })

在此处输入图片说明

but, my problem is when i try to put $scope.relatorias , inside of $scope.data.

This is $scope.relatorias , this variable has the data of every consultor group by month, like this:

在此处输入图片说明

If you open each array look like this

在此处输入图片说明

I just need push ganancias_netas, but my problem is when there is an empty month, for example anapaula has data in every month but renato hasn't.

I have try to user for or for each but is doesn't work, I'm not an expert in matrix and this is my first time working on it.

fiddle: http://fiddle.jshell.net/rfcabal/5ftw7c8d/

/// UPDATE ///

I added this code that first fill with 0 $scope.data and then search for the values in relatorios and shoudl fill $scope.data, but for some reason jus fill with the last found value.

for (var i = 1; i < $scope.data.length; i++) {
                for (var a = 1; a < $scope.data[0].length; a++) {
                    $scope.data[i][a] = 0;
                    for (var b = 0; b < $scope.relatorios[a-1].length; b++) {
                        console.log(a-1+' '+b+' '+3);
                        console.log($scope.relatorios[a-1][b]['ganancias_netas'])
                        $scope.data[i][a] = $scope.relatorios[a-1][b]['ganancias_netas'];                   
                    }
                }
            }

Thanks for your help

I just solved with 2 for

First i fill every data space with 0

for (var i = 1; i < $scope.data.length; i++) {
                for (var a = 1; a < $scope.data[0].length; a++) {
                    $scope.data[i][a] = 0;
                }
            }

The i jus remplace where fecha_emision equal to position 1 of every array.

for (var a = 0; a < $scope.relatorios.length; a++) {
                for (var b = 0; b < $scope.relatorios[a].length; b++) {
                    for (var i = 1; i < $scope.data.length; i++) {
                        var index = $scope.data[i].indexOf($scope.relatorios[a][b]['fecha_emision']);
                        if(index >= 0) {
                            $scope.data[i][a+1] = parseFloat($scope.relatorios[a][b]['ganancias_netas']);
                        }
                    }
                }                                       
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM