我遇到了一个问题,我应该根据'&&'来过滤数据给出的输入条件。
要过滤的数据
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"]
过滤应该通过比较“是”值并使用输入中的“AND”条件来完成
export function filterWithMultipleIssueData(ssData, filterData){
let filteredSSData=[]
ssData.map((row,index)=>{
filterData.map((field)=>{
if(row[field] == 'yes'){
filteredSSData.push(row.toJS())
}
})
})
return filteredSSData
}
在上面的函数中,我使用'OR'条件得到过滤的SSData。(OnPower'OR'hazard'Or'OnGenerator)
比较应该与OnPower'和'危险'和'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"
},
}
答案 0 :(得分:1)
不是在过滤时使用map
,而是可以使用every
代替:
function filterYes(data, keys){
return data.filter(data => keys.every(key => data[key] === "yes"));
}
我猜你的数据是一个数组(导致你调用map
),否则它会更复杂一些:
function filterYes(data, key){
return Object.assign({}, ...Object.entries(data).filter(([key, value]) =>
keys.every(key => value[key] === "yes")
).map(([key, value]) => ({[key]: value}));
}
答案 1 :(得分:1)
以下是使用higher-order
函数执行此操作的方法:
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));
答案 2 :(得分:0)
您没有数组,但可以使用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}), {});
点击演示:
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);
答案 3 :(得分:-1)
我在这里看到多个答案。一个接近您使用的代码结构!
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);
});
}