Reputation: 3311
My json data is here
[
{
"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"
}
]
My param like this
["", "2", "1", "0", "0", "0"]
My JS is here
$.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])
})
})
})
I want to filter all param with AND condition. now it's doing like OR
Upvotes: 0
Views: 1636
Reputation: 30675
I'm guessing you wish to filter using param as the list of field values in the following order: hf_section, design_id, genre_id, color_id, price.
I'm presuming that when param[n] is 0 the field value should be ignored.
In this case we can try this approach:
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"]));
Upvotes: 2
Reputation: 386570
I suggest to use an object
filter = { hf_section: '2', design_id: '1', genre_id: '0', color_id: '0', price: '0' }
with keys who are corresponding with the keys of the given data and the wanted values for filtering.
Then you could filter the keys to get only the wanted result.
Filtering the keys and filtering the objects could be easier, if you take numbers instead of strings for comparisons.
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; }
Upvotes: 2