【问题标题】:Lodash: filter multiple propertiesLodash:过滤多个属性
【发布时间】:2020-04-27 14:41:07
【问题描述】:

我是 lodash 的新手。

我在使用 lodash 过滤时遇到问题。我有一个深层嵌套的 json 对象,如果 productName = 'Lotto' 和 board selectionMethod = "AUTOPICK",我想过滤它

当我尝试以下解决方案时,它会返回所有结果而不是过滤。我尝试了多种方法来做到这一点,但我总是得到所有结果。

谁能给点建议?

var results = {

"buyTicketDetails": {
    "result": 0,
    "message": "Success",
    "product": [
        {
            "productName": "Lotto",
            "displayPromoMessage": false,
            "drawDetails": [
                {
                    "drawTypeDescription": "REGULAR DRAW",
                    "drawAttribute": "EVENING",
                    "drawStartDate": "2019-01-12T00:00:00.000-05",
                    "drawEndDate": "2019-01-12T00:00:00.000-05"
                },
                {
                    "drawTypeDescription": "SPECIAL DRAW",
                    "drawAttribute": "EVENING",
                    "drawStartDate": "2019-01-12T00:00:00.000-05",
                    "drawEndDate": "2019-01-12T00:00:00.000-05"
                }
            ],
            "board": [
                {
                    "boardType": "REGULAR",
                    "selectionMethod": "AUTOPICK",
                    "selectionSet": [
                        "2",
                        "4",
                        "10",
                        "12",
                        "17",
                        "31"
                    ]
                },
                {
                    "boardType": "RAFFLE",
                    "selectionMethod": "SYSTEMPICK",
                    "selectionSet": [
                        "40001722-01"
                    ]
                }
            ]
        },
        {
            "productName": "Encore",
            "displayPromoMessage": false,
            "drawDetails": [
                {
                    "drawTypeDescription": "REGULAR DRAW",
                    "drawAttribute": "EVENING",
                    "drawStartDate": "2019-01-12T00:00:00.000-05",
                    "drawEndDate": "2019-01-12T00:00:00.000-05"
                }
            ],
            "board": [
                {
                    "boardType": "REGULAR",
                    "selectionMethod": "SYSTEMPICK",
                    "selectionSet": [
                        "3440514"
                    ]
                }
            ]
        }
    ]
  }
}    

const filterCat = _.filter(results, { product: [
{ 
    productName: "Lotto", 
    board: {
        selectionMethod: "AUTOPICK"
    }}
    ] 
}
);

console.log(filterCat);

【问题讨论】:

    标签: lodash


    【解决方案1】:

    使用纯 JS。

    您也可以使用 Javascript 的过滤功能来做到这一点。

    filter函数实际上是作用在Arrays上的,所以我们先用map循环将对象添加到数组中,然后再用filter函数只获取我们需要的数据!

    let Product = results.buyTicketDetails.product
    
    let getSelectionMethods=(index) => Product[index].board.map((d,i)=>d.selectionMethod)
    
    let  getTargetedProducts =(Name,Method)=> Product.map((d,i)=>{
      if(Product[i].productName==Name && getselectionMethods(i).indexOf(Method) !==-1){
                return d
        }
    })
    
    
    let FilteredProducts = getTargetedProducts("Lotto","AUTOPICK").filter((d)=>d !==undefined)
    
    console.log(FilteredProducts)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 2016-03-30
      相关资源
      最近更新 更多