我的json数据在这里
[
{
"id":"13167",
"photo_cd":"9946",
"src":"",
"price":"5990",
"hf_section":"1",
"sell_price":"6469",
"design_id":"1",
"genre_id":"1",
"color_id":"1"
},
{
"id":"13166",
"photo_cd":"9945",
"src":"",
"price":"5990",
"hf_section":"1",
"sell_price":"6469",
"design_id":"3",
"genre_id":"1",
"color_id":"1"
},
{
"id":"13165",
"photo_cd":"9944",
"src":"",
"price":"5990",
"hf_section":"1",
"sell_price":"6469",
"design_id":"1",
"genre_id":"1",
"color_id":"4"
},
{
"id":"13164",
"photo_cd":"9943",
"src":"",
"price":"6990",
"hf_section":"1",
"sell_price":"7549",
"design_id":"2",
"genre_id":"3",
"color_id":"3"
}
]
我的参数是这样的
["", "2", "1", "0", "0", "0"]
我的JS在这里
$.getJSON(
filepath,
function(data) {
var keys = [];
var values = [];
if (param[1] != 0) {
keys.push("hf_section");
values.push(param[1]);
}
if (param[2] != 0) {
keys.push("design_id");
values.push(param[2]);
}
if (param[3] != 0) {
keys.push("genre_id");
values.push(param[3]);
}
if (param[4] != 0) {
keys.push("color_id");
values.push(param[4]);
}
if (param[5] != 0) {
keys.push("price");
values.push(param[5]);
}
var result = data.filter(function(e) {
return keys.every(function(a) {
return values.includes(e[a])
})
})
})
我想用AND条件过滤所有参数。现在它就像OR
答案 0 :(得分:2)
我建议使用一个物体
filter = { hf_section: '2', design_id: '1', genre_id: '0', color_id: '0', price: '0' }
具有与给定数据的键和用于过滤的所需值相对应的键。
然后,您可以过滤键以仅获取所需的结果。
如果您使用数字而不是字符串进行比较,则过滤键和过滤对象可能会更容易。
var data = [{ id: "13167", photo_cd: "9946", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "1", genre_id: "1", color_id: "1" }, { id: "13166", photo_cd: "9945", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "3", genre_id: "1", color_id: "1" }, { id: "13165", photo_cd: "9944", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "1", genre_id: "1", color_id: "4" }, { id: "13164", photo_cd: "9943", src: "", price: "6990", hf_section: "1", sell_price: "7549", design_id: "2", genre_id: "3", color_id: "3" }],
items = ["", "2", "1", "0", "0", "0"],
filter = { hf_section: '2', design_id: '1', genre_id: '0', color_id: '0', price: '0' },
keys = Object.keys(filter).filter(k => +filter[k]),
result = data.filter(o => keys.every(k => o[k] == filter[k]));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:2)
我猜您希望按照以下顺序使用param作为字段值列表进行过滤:hf_section,design_id,genre_id,color_id,price。
我假设当param [n]为0时,字段值应该被忽略。
在这种情况下,我们可以尝试这种方法:
let testData = [{ id: "13167", photo_cd: "9946", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "1", genre_id: "1", color_id: "1" }, { id: "13166", photo_cd: "9945", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "3", genre_id: "1", color_id: "1" }, { id: "13165", photo_cd: "9944", src: "", price: "5990", hf_section: "1", sell_price: "6469", design_id: "1", genre_id: "1", color_id: "4" }, { id: "13164", photo_cd: "9943", src: "", price: "6990", hf_section: "1", sell_price: "7549", design_id: "2", genre_id: "3", color_id: "3" }];
let fields = ["photo_cd", "hf_section", "design_id", "genre_id", "color_id", "price"];
function filterDataItems(data, filterParams) {
return data.filter(item => {
return fields.every((fieldName, index) => {
return filterParams[index] == 0 || (item[fieldName] == filterParams[index]);
});
});
}
console.log('Test 1: ', filterDataItems(testData, ["9946", "1", "1", "1", "0", "5990"]));
console.log('Test 2: ', filterDataItems(testData, ["0", "0", "0", "0", "4", "0"]));
console.log('Test 3: ', filterDataItems(testData, ["0", "0", "0", "0", "0", "0"]));
console.log('Test 4: ', filterDataItems(testData, ["0", "0", "0", "0", "0", "0"]));