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

Filtering data using 'AND' condition of inputs given

我遇到一个问题,我应该根据给定输入的“&&”条件过滤数据。

要过滤的数据

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"]

过滤应该通过与 "yes" 值比较并使用来自输入

的 "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' 条件获取 filteredSSData。(OnPower 'OR' hazard 'OR' 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"
      },
}

而不是在过滤时使用 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}));
}

您没有数组,但您可以使用 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);

以下是使用 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));