Lodash:过滤多个属性

时间:2020-01-09 23:02:10

标签: lodash

我是lodash的新手。

我无法使用lodash进行过滤。我有一个深层嵌套的json对象,如果productName ='Lotto'并且面板选择方法=“ 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);

1 个答案:

答案 0 :(得分:0)

使用Pure JS。

您也可以使用Javascript的过滤器功能执行此操作。

filter函数实际上在数组上起作用,因此我们先使用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)