//商品列表vardata=[{"id":0,"goodsImg":"/images/ss1.jpg","title":"手机","subt...
//商品列表
var data = [
{
"id": 0,
"goodsImg": "/images/ss1.jpg",
"title": "手机",
"subtitle": "手机,上海,粉色",
"price": "888.00",
"sale": "999.00",
"place": "北京",
"color": "红色",
"pid": "华为",
"sid": "手机"
},
{
"id": 1,
"goodsImg": "/images/ss1.jpg",
"title": "手机",
"subtitle": "手机,上海,粉色",
"price": "888.00",
"sale": "999.00",
"place": "北京",
"color": "黑色",
"pid": "华为",
"sid": "手机"
},
{
"id": 2,
"goodsImg": "/images/ss1.jpg",
"title": "手机",
"subtitle": "手机,上海,粉色",
"price": "888.00",
"sale": "999.00",
"place": "上海",
"color": "红色",
"pid": "苹果",
"sid": "手机"
},
{
"id": 3,
"goodsImg": "/images/ss1.jpg",
"title": "手机",
"subtitle": "手机,上海,粉色",
"price": "888.00",
"sale": "999.00",
"place": "北京",
"color": "黑色",
"pid": "苹果",
"sid": "手机"
}
]
//筛选条件,需要筛选的字段和商品的字段得保持一致
var filter = {
pid: ['华为'],
sid: ['手机'],
color: ['黑色', '红色'],
place: ['北京']
}
function filterData(data, filter){
return data.filter(item => {
var isIn = true
for(var key in filter) {
if(filter[key].indexOf(item[key]) < 0){
isIn = false
break
}
}
return isIn
})
}
var newData = filterData(data, filter)
console.log(newData)
全文详见:http://xpxw.com/?id=91