Comparing angular arrays

1

I am creating a device for cell ... so I had the following problem: I need to compare if an account has not been paid, and send a warning ...

accounts should be paid every month .. if you can not find the name in the other array generate the warning

[{"tipo":0,"classe":"Entrada","nome":"111","categoria":"Meu salario","cor":"MediumSeaGreen"}]

conference account list if paid ...

[{"classe":"Entrada","data":"10-11-2016","dia":"Qua","tipo":0,"categoria":"Meu salario","cor":"MediumSeaGreen","nome":"111","comentario":"","total":5},{"classe":"Saída","data":"10-11-2016","dia":"Qua","tipo":1,"nome":"1","categoria":"Despesa fixa variavel","cor":"DodgerBlue","total":1}]
    
asked by anonymous 10.02.2016 / 21:50

1 answer

1

You can use the forEach method to go through each element of array and inside it to do the verification.

Example:

var obj1 = [{"tipo":0,"classe":"Entrada","nome":"111","categoria":"Meu salario","cor":"MediumSeaGreen"}]
var obj2 = [{"classe":"Entrada","data":"10-11-2016","dia":"Qua","tipo":0,"categoria":"Meu salario","cor":"MediumSeaGreen","nome":"111","comentario":"","total":5},{"classe":"Saída","data":"10-11-2016","dia":"Qua","tipo":1,"nome":"1","categoria":"Despesa fixa variavel","cor":"DodgerBlue","total":1}]

//Verificação
angular.forEach(obj1, function(item1, key) {
    angular.forEach (obj2, function(item2, key) {
        if(item1.categoria == item2.categoria) {
            item1.pago = true;
        }
    })
})

What happens is that in this part: angular.forEach(obj1, function(item1, key) for each element of array obj1 it is identified by item1 , so you can access each property through item1.propriedade . key refers to the $index of the object within that array , if it is necessary to use, it is there. The same logic applies to the second array .

Note that I'm doing 2 angular.forEach because you need to navigate 2 array , so for every element of the first array , you need to compare with every element of the second array .

The treatment of item1.pago = true was an example, but it is there that you apply the status of 'Paid' as best for you.

    
10.02.2016 / 22:24