动态进行数组的多条件筛选

139 阅读1分钟

动态进行数组的多条件筛选,其中搜索条件为队形类型的数据,key为要进行搜索的条件在数据中对应的自字段,值为满足条件的值。

{second_type: "2", type_id: "6", series_id: ""}

function searchGoods (searchValue) {
    //contract_goods_data为要进行筛选的数组数据
    //searchValue为搜索条件
    //searchValue : {second_type: "2", type_id: "6", series_id: ""}
    let secharData = multiFilter(contract_goods_data,searchValue)
    table.reload('table-contract-list', {
        data:secharData
    });
    return false;
}

 function multiFilter(array, filters) {
    const filterKeys = Object.keys(filters)
    // 过滤所有元素
    return array.filter((item) => {
        // 动态验证所有的过滤标准
        return filterKeys.every((key) => {
            if (!filters[key] || !filters[key].length) return true
            return filters[key] == item[key]
        })
    })
}