【问题标题】:lodash dropWhile not workinglodash drop虽然不工作
【发布时间】:2016-02-26 12:13:22
【问题描述】:

我用 lodash 编写了这个函数,但有时它可以正常工作而其他人则不行,它写得很好还是我的语法有误?

    myApp.controller('geoCtrl', function($scope, Canchas){

    var filtro = [];

    $scope.filtroJugadores = function(){

        if($scope.j5===true){
            var j5 = _(filtro).push({'jugadores5':true});
            j5 = j5.commit();
        }else{
            _.dropWhile(filtro, ['jugadores5', true]);
            //index = _.indexOf(filtro, _.find(filtro, {'jugadores5':true}));
            //if (index != -1){filtro.splice(index, 1, {'jugadores5':false})};
        }

        if($scope.j9===true){
            var j9 = _(filtro).push({'jugadores9':true});
            j9 = j9.commit();
        }else{
            _.dropWhile(filtro, ['jugadores9', true]);

            //index = _.indexOf(filtro, _.find(filtro, {'jugadores9':true}));
            //if (index != -1){filtro.splice(index, 1, {'jugadores9':false})};
        }

        filtro = _.uniqBy(filtro, 'jugadores5');
        filtro = _.uniqBy(filtro, 'jugadores9');

        console.log(filtro);
    }
});

plunker

【问题讨论】:

  • 你能说得更具体点吗?
  • 当你点击一个复选框 filtroJugadores 调用函数。如果为真,请在数组中插入对象 { 'jugador5' : true} ,如果为假,则必须将其删除。如果我使用多个复选框,出现故障,或者它没有删除对象,或者我将其保留为 true

标签: angularjs function lodash


【解决方案1】:

使用_.uniqWith

    myApp.controller('geoCtrl', function($scope, Canchas){

    var filtro = [];

    $scope.filtroJugadores = function(){
        filtro = _.uniqWith(filtro, _.isEqual);
        if($scope.j5===true){
            var j5 = _(filtro).push({'jugadores5':true});
            j5 = j5.commit();
        }else{
            if(_.some(filtro, {'jugadores5':true})) {
                _.dropWhile(users, ['jugadores5', true]);
            }
        }

        if($scope.j9===true){
            var j9 = _(filtro).push({'jugadores9':true});
            j9 = j9.commit();
        }else{
            if(_.some(filtro, {'jugadores9':true})) {
                _.dropWhile(filtro, ['jugadores9', true]);
            }
        }
        console.log(filtro);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 2019-11-09
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多