相关链接
js对二维数组的精确和模糊筛选并输出的封装函数
被筛选数组
var data = [
{"name": "《西游记》", "author": "吴承恩", "cat": "A级书刊"},
{"name": "《三国演义》", "author": "罗贯中", "cat": "A级书刊"},
{"name": "《红楼梦》", "author": "曹雪芹", "cat": "B级书刊"}
];
封装过滤条件
//筛选条件封装函数-精确匹配;
function filterData(data, name, author) {
var filters = [
o => o.name.toString().includes(name),
o => o.cat.includes(author)
];
var result = data.filter(o => filters.every(fn => fn(o)));
return result;
}
打印结果
var name = "", author = "A级";
console.log(filterData(data, name, author));
@lockdata.cn