I am facing an issue where I should filter the data based on '&&' condition of inputs given.
The data to be filtered
let ssData={
"0": {
"OnPower": "no",
"hazard": "no",
"issues": "no",
"OnGenerator": "no",
"PermanentFuel": "no",
"onDamage": "no"
},
"1": {
"OnPower": "yes",
"hazard": "yes",
"issues": "no",
"OnGenerator": "yes",
"PermanentFuel": "no",
"onDamage": "yes"
},
"2": {
"OnPower": "no",
"hazard": "no",
"issues": "no",
"OnGenerator": "yes",
"PermanentFuel": "yes",
"onDamage": "no"
},
"3": {
"OnPower": "yes",
"hazard": "yes",
"issues": "yes",
"OnGenerator": "yes",
"PermanentFuel": "yes",
"onDamage": "yes"
},
"4": {
"OnPower": "no",
"hazard": "yes",
"issues": "no",
"OnGenerator": "no",
"PermanentFuel": "no",
"onDamage": "yes"
},
"5": {
"OnPower": "yes",
"hazard": "no",
"issues": "no",
"OnGenerator": "yes",
"PermanentFuel": "no",
"onDamage": "no"
},
"6": {
"OnPower": "yes",
"hazard": "no",
"issues": "yes",
"OnGenerator": "no",
"PermanentFuel": "no",
"onDamage": "yes"
}
}
input to filtering is :
let filterData= ["OnPower","hazard","OnGenerator"]
The filtering should be done by comparing with "yes" value and using "AND" condition from input
export function filterWithMultipleIssueData(ssData, filterData){
let filteredSSData=[]
ssData.map((row,index)=>{
filterData.map((field)=>{
if(row[field] == 'yes'){
filteredSSData.push(row.toJS())
}
})
})
return filteredSSData
}
In the above function , Im getting filteredSSData using 'OR' condition.(OnPower 'OR' hazard 'OR' OnGenerator)
The Comparision should be with OnPower 'AND' hazard 'AND' OnGenerator
The expected Output is:
{
"1": {
"OnPower": "yes",
"hazard": "yes",
"issues": "no",
"OnGenerator": "yes",
"PermanentFuel": "no",
"onDamage": "yes"
},
"3": {
"OnPower": "yes",
"hazard": "yes",
"issues": "yes",
"OnGenerator": "yes",
"PermanentFuel": "yes",
"onDamage": "yes"
},
}
Instead of using map
on filtered you might wana use every
instead:
function filterYes(data, keys){
return data.filter(data => keys.every(key => data[key] === "yes"));
}
I guess your data is an array (cause you call map
on it) otherwise its a bit more complicated:
function filterYes(data, key){
return Object.assign({}, ...Object.entries(data).filter(([key, value]) =>
keys.every(key => value[key] === "yes")
).map(([key, value]) => ({[key]: value}));
}
Here's how to do it with higher-order
functions:
let ssData={ "0": { "OnPower": "no", "hazard": "no", "issues": "no", "OnGenerator": "no", "PermanentFuel": "no", "onDamage": "no" }, "1": { "OnPower": "yes", "hazard": "yes", "issues": "no", "OnGenerator": "yes", "PermanentFuel": "no", "onDamage": "yes" }, "2": { "OnPower": "no", "hazard": "no", "issues": "no", "OnGenerator": "yes", "PermanentFuel": "yes", "onDamage": "no" }, "3": { "OnPower": "yes", "hazard": "yes", "issues": "yes", "OnGenerator": "yes", "PermanentFuel": "yes", "onDamage": "yes" }, "4": { "OnPower": "no", "hazard": "yes", "issues": "no", "OnGenerator": "no", "PermanentFuel": "no", "onDamage": "yes" }, "5": { "OnPower": "yes", "hazard": "no", "issues": "no", "OnGenerator": "yes", "PermanentFuel": "no", "onDamage": "no" }, "6": { "OnPower": "yes", "hazard": "no", "issues": "yes", "OnGenerator": "no", "PermanentFuel": "no", "onDamage": "yes" } }; let filterData= ["OnPower","hazard","OnGenerator"]; function filterDataFunction(dataToFilter, filterOptions) { return Object.getOwnPropertyNames(dataToFilter) .map(elem => ssData[elem]) // convert object to array .filter(data => filterOptions .map(filter => data[filter] === 'yes') // filter data where specified answers equal yes .reduce((first, second) => first && second)); // perform && on all elements of the array } console.log(filterDataFunction(ssData, filterData));
You don't have an Array, but you can do this with Object.entries().filter().reduce()
.
let result = Object.entries(ssData)
.filter(([_, o]) => filterData.every(k => o[k] === "yes"))
.reduce((res, [k, o]) => Object.assign(res, {[k]: o}), {});
Click for demo:
let ssData = { "0": { "OnPower": "no", "hazard": "no", "issues": "no", "OnGenerator": "no", "PermanentFuel": "no", "onDamage": "no" }, "1": { "OnPower": "yes", "hazard": "yes", "issues": "no", "OnGenerator": "yes", "PermanentFuel": "no", "onDamage": "yes" }, "2": { "OnPower": "no", "hazard": "no", "issues": "no", "OnGenerator": "yes", "PermanentFuel": "yes", "onDamage": "no" }, "3": { "OnPower": "yes", "hazard": "yes", "issues": "yes", "OnGenerator": "yes", "PermanentFuel": "yes", "onDamage": "yes" }, "4": { "OnPower": "no", "hazard": "yes", "issues": "no", "OnGenerator": "no", "PermanentFuel": "no", "onDamage": "yes" }, "5": { "OnPower": "yes", "hazard": "no", "issues": "no", "OnGenerator": "yes", "PermanentFuel": "no", "onDamage": "no" }, "6": { "OnPower": "yes", "hazard": "no", "issues": "yes", "OnGenerator": "no", "PermanentFuel": "no", "onDamage": "yes" } }; let filterData= ["OnPower","hazard","OnGenerator"]; let result = Object.entries(ssData) .filter(([_, o]) => filterData.every(k => o[k] === "yes")) .reduce((res, [k, o]) => Object.assign(res, {[k]: o}), {}); console.log(result);
I see here multiple answers. One close to the code structure you used!
function filterWithMultipleIssueData(ssData, filterData){
let filteredSSData= [];
Object.keys(ssData).forEach((key)=> {
let row = ssData[key];
let filtered = filterData.filter(field => row[field] == 'yes');
if(filtered.length == filterData.length) //filterData is the array you provided
filteredSSData.push(row);
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.