在 Angular 中使用 LoDash 从 localData 中查找对象

Use LoDash in Angular to find objects from localData

我是 LoDash 的新手。我只是想在 localData 中找到一个对象,但我一直失败。我不知道问题出在哪里。我可以使用 _chain 和 fratten 函数来过滤我的对象,但是在我应用 find 之后,出现错误:"Uncaught SyntaxError: Unexpected token".

下面是我的代码:你们能帮忙吗?你能否也让我知道,我如何确保展平功能正常工作?非常感谢。

angular.module('myApp', [])
    .controller('myTeamsCtrl', [function(){
        var self = this;

        var data = {"standings":[
            {
                "divisionName":"5th Grade - Green",
                "divisionStandings":[
                    {"teamId":3275,"teamName":"Severn Elite Gold","wins":3,"losses":1,"winningPct":".750","pointsFor":141,"pointsAgainst":126,"pointsDiff":15},
                    {"teamId":3220,"teamName":"HC Elite OMalley 5th","wins":3,"losses":1,"winningPct":".750","pointsFor":196,"pointsAgainst":191,"pointsDiff":5},
                    {"teamId":3277,"teamName":"MD 3D 5th","wins":2,"losses":2,"winningPct":".500","pointsFor":156,"pointsAgainst":132,"pointsDiff":24},
                    {"teamId":3222,"teamName":"Columbia Ravens 5th","wins":2,"losses":2,"winningPct":".500","pointsFor":147,"pointsAgainst":124,"pointsDiff":23},
                    {"teamId":3224,"teamName":"Team Dedication 5th","wins":2,"losses":2,"winningPct":".500","pointsFor":113,"pointsAgainst":123,"pointsDiff":-10},
                    {"teamId":4299,"teamName":"D1 Spartans","wins":0,"losses":0,"winningPct":".000","pointsFor":0,"pointsAgainst":0,"pointsDiff":0}
                ]
            },
            {
                "divisionName":"5th Grade - White",
                "divisionStandings":[
                    {"teamId":3276,"teamName":"HC Elite 4th Tookes","wins":4,"losses":1,"winningPct":".800","pointsFor":187,"pointsAgainst":158,"pointsDiff":29},
                    {"teamId":3225,"teamName":"CBSA Hoyas 5th Grade","wins":2,"losses":2,"winningPct":".500","pointsFor":149,"pointsAgainst":133,"pointsDiff":16},
                    {"teamId":3226,"teamName":"Randelstown Runnin Rebels 5th","wins":1,"losses":3,"winningPct":".250","pointsFor":108,"pointsAgainst":155,"pointsDiff":-47},
                    {"teamId":3223,"teamName":"Up n Coming Clippers 5th","wins":0,"losses":0,"winningPct":".000","pointsFor":0,"pointsAgainst":0,"pointsDiff":0},
                    {"teamId":3221,"teamName":"HC Elite Knisley 5th","wins":0,"losses":5,"winningPct":".000","pointsFor":163,"pointsAgainst":218,"pointsDiff":-55}
                ]
            }
        ]};
        var teamId = '3221';
        var standingTeam = _.chain(data.standings)
                        .flatten("divisionStandings")
                        .find({'teamId', teamId})
                        .value();



        console.log(standing);

    }])

flatten用于将一组数组合并为一个数组(例如[[1],[2]] -> [1,2])。

您似乎在尝试获取所有部门的所有排名,然后通过 id 找到一个排名。方法如下:

_.chain(data.standings)
.pluck('divisionStandings')
.flatten()
.findWhere({ 'teamId': teamId })
.value()

您的代码中有两个错误:

  1. 您无法找到值为字符串的数字 teamId
  2. find 函数中的对象格式不正确。

    teamId = 3221;
    
    standingTeam = _.chain(data.standings)
      .pluck('divisionStandings')
      .flatten()
      .find({ 'teamId': teamId })
      .value();
    
    console.log(standingTeam);