【问题标题】:How to get group object when the value of property is equal to itemSearch当属性的值等于 itemSearch 时如何获取组对象
【发布时间】:2016-04-06 05:11:26
【问题描述】:

当具有 [array] 值的一个属性是 itemSearch 时,如何获取组 {object}, 我使用 _.lodash/下划线

{
"tileRecords"   : [
{   
"tileName"      :   "Fama Brown",   
"tileGroup"     :   ["Polished", "Matt", "Rought"],
"tileDetails"   :   
        {
        "tileSize"          :   "60x60",
        "tileType"          :   "Polished"
        }
},
{   
"tileName"      :   "Fama Nero",    
"tileGroup"     :   ["Polished", "Matt", "Rought"],
"tileDetails"   :   
        {
        "tileSize"          :   "60x60",
        "tileType"          :   "Polished"
        }
},
{   
"tileName"      :   "Dolce Beige",  
"tileGroup"     :   ["Italian", "Matt", "Rought"],
"tileDetails"   :   
        {
        "tileSize"          :   "60x60",
        "tileType"          :   "Polished"
        }
}
] 
}

如何使用 tileGroup 中值为“Polished”的过滤器获取对象 tileName : Fama Brown 和 Fama Nero?

因为该组的唯一值是 Polished。

【问题讨论】:

    标签: javascript angularjs json underscore.js lodash


    【解决方案1】:

    您可以使用angular filter 代替 lodash,这很容易实现, 如果您将其与 ng-repeat 一起使用,请尝试以下操作,

    <div ng-repeat="tile in tiles.tileRecords | pick: tileGroupFilter">
      {{ tile.tileName }}
    </div>
    

    在你的控制器中

        $scope.tiles = {
                           "tileRecords"   : [
                              {   
                                 "tileName"      :   "Fama Brown",   
                                 "tileGroup"     :   ["Polished", "Matt", "Rought"],
                                 "tileDetails"   :   
                                     {
                                         "tileSize"          :   "60x60",
                                         "tileType"          :   "Polished"
                                     }
                               },
                               {   
                                  "tileName"      :   "Fama Nero",    
                                  "tileGroup"     :   ["Polished", "Matt", "Rought"],
                                  "tileDetails"   :   
                                      {
                                          "tileSize"          :   "60x60",
                                          "tileType"          :   "Polished"
                                       }
                                },
                                {   
                                    "tileName"      :   "Dolce Beige",  
                                    "tileGroup"     :   ["Italian", "Matt", "Rought"],
                                    "tileDetails"   :   
                                        {
                                            "tileSize"          :   "60x60",
                                            "tileType"          :   "Polished"
                                        }
                                 }
                            ] 
                       };
    
    
    $scope.tileGroupFilter = function(elm) {
      return (elem.tileGroup.indexOf("Polished") > 0) ;
    }
    

    【讨论】:

    • 什么是pick:在过滤器中?它是如何工作的?对不起,这里还是新的角度
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2020-07-27
    相关资源
    最近更新 更多