AngularJS filter by arrangement on property of the object

0

I'm trying to apply a filter inside an ng-if, but I do not come up with the solution.

I have an arrangement of objects of this type:

tiempos = [
    {
        id: 39,
        nombre: "Juan",
        tiempos: [
            {
                vuelta: 1,
                tiempo: "16.15",
                terminado: 1
            },
            {
                vuelta: 2,
                tiempo: "16.07",
                terminado: 1
            }
        ]
    },
    {
        id: 40,
        nombre: "Pedro",
        tiempos: [
            {
                vuelta: 1,
                tiempo: "18.14",
                terminado: 1
            },
            {
                vuelta: 2,
                tiempo: "0",
                terminado: 0
            }
        ]
    },
    {
        id: 41,
        nombre: "Paco",
        tiempos: [
            {
                vuelta: 1,
                tiempo: "17.32",
                terminado: 1
            },
            {
                vuelta: 2,
                tiempo: "0",
                terminado: 0
            }
        ]
    },
]

What I need is to filter by the "finished" property that is equal to 1 of the "second element" of the property "times".

That is, I only show the elements that have "finished: 1" in the second path, for this example that only shows the object with id 39.

Using

ng-if="(tiempos | filter: {})"

In advance, thanks for your time.

    
asked by Agares 05.10.2018 в 15:27
source

2 answers

0

According to the response of @PabloLozano I have taken into account that you use angularJS , I do not know if this is what you need, I remain attentive.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {

    $scope.tiempos = [
        {
            id: 39,
            nombre: "Juan",
            tiempos: [
                {
                    vuelta: 1,
                    tiempo: "16.15",
                    terminado: 1
                },
                {
                    vuelta: 2,
                    tiempo: "16.07",
                    terminado: 1
                }
            ]
        },
        {
            id: 40,
            nombre: "Pedro",
            tiempos: [
                {
                    vuelta: 1,
                    tiempo: "18.14",
                    terminado: 1
                },
                {
                    vuelta: 2,
                    tiempo: "0",
                    terminado: 0
                }
            ]
        },
        {
            id: 41,
            nombre: "Paco",
            tiempos: [
                {
                    vuelta: 1,
                    tiempo: "17.32",
                    terminado: 1
                },
                {
                    vuelta: 2,
                    tiempo: "0",
                    terminado: 0
                }
            ]
        },
    ]

    $scope.resultado=$scope.tiempos.filter(elem => elem.tiempos[1] && elem.tiempos[1].terminado===1);
    console.log($scope.resultado);

});
<!DOCTYPE html>
<html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <body>
        <div ng-app="myApp" ng-controller="myCtrl">
            <div ng-if="(tiempos|filter:resultado).length == 0">
                {{ resultado }}
            </div>
        </div>
    </body>
</html>
Update

I have updated the answer by adding the following in html

ng-if="(tiempos|filter:resultado).length == 0"
    
answered by 05.10.2018 / 16:19
source
3

Precisely there is the method filter in the Array class that does just what you need:

const tiempos = [
    {
        id: 39,
        nombre: "Juan",
        tiempos: [
            {
                vuelta: 1,
                tiempo: "16.15",
                terminado: 1
            },
            {
                vuelta: 2,
                tiempo: "16.07",
                terminado: 1
            }
        ]
    },
    {
        id: 40,
        nombre: "Pedro",
        tiempos: [
            {
                vuelta: 1,
                tiempo: "18.14",
                terminado: 1
            },
            {
                vuelta: 2,
                tiempo: "0",
                terminado: 0
            }
        ]
    },
    {
        id: 41,
        nombre: "Paco",
        tiempos: [
            {
                vuelta: 1,
                tiempo: "17.32",
                terminado: 1
            },
            {
                vuelta: 2,
                tiempo: "0",
                terminado: 0
            }
        ]
    },
]

const resultado=tiempos.filter(elem => elem.tiempos[1] && elem.tiempos[1].terminado===1);
console.log(resultado);
    
answered by 05.10.2018 в 15:34