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}));
}