简体   繁体   English

使用给定输入的“AND”条件过滤数据

[英]Filtering data using 'AND' condition of inputs given

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过滤应该通过与“是”值进行比较并使用来自输入的“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
}

In the above function , Im getting filteredSSData using 'OR' condition.(OnPower 'OR' hazard 'OR' OnGenerator)在上面的函数中,我使用“OR”条件获取过滤的SSData。(OnPower 'OR' hazard 'OR' OnGenerator)

The Comparision should be with OnPower 'AND' hazard 'AND' OnGenerator比较应该与 OnPower 'AND' 危险 '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:而不是使用的map上过滤你可能WANA使用every来代替:

 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:我猜你的数据是一个数组(因为你在它上面调用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}));
}

Here's how to do it with higher-order functions:以下是使用higher-order函数的方法:

  • map地图
  • filter筛选
  • reduce降低

 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() .你没有数组,但你可以用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);
    }); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM