使用'&&&条款

时间:2018-02-13 16:42:02

标签: javascript jquery arrays filter

我有一个与来自不同api的位置相关的数据,需要根据位置选择过滤另一组数据。

let locationData= [
            {
            "Name": "Olympics",
            "Problem Type": "Access",
            "LS_ID": "421"
        },
        {
            "Name": "Olympics",
            "Problem Type": "Route Issue",
            "LS_ID": "420"
        },
        {
            "Name": "Olympics",
            "Problem Type": "Tools",
            "LS_ID": "420"
        },
        {
            "Name": "Olympics",
            "Problem Type": "Access",
            "LS_ID": "420"
        },
        {
            "Name": "Olympics",
            "Problem Type": "Tools",
            "LS_ID": "422"
        }       
    ]

需要过滤的主数据是

 let mainData=
 [
 {
  "SiteType": "Outdoor",
  "ID": "421", 
  "Name": "Olympics"     
},
 {

  "SiteType": "Indoor",
  "ID": "420", 
  "Name": "Olympics"     
},
 {

  "SiteType": "International",
  "ID": "422", 
  "Name": "Olympics"    
},
 {

  "SiteType": "Local",
  "ID": "423",  
  "Name": "Olympics"    
}
]

我们有一个多选字段,我们将选择'问题类型' 选项(例如:访问权限,工具,无条目,路线问题等)

因此,在选择时,我们会获得一些字符串,例如 ['访问','工具']

要求是显示具有('访问' AND'工具')问题类型的SiteType,即;来自上面的例子

如果输入是

  let selections=['Access','Tools']

预期输出

{

  "SiteType": "Indoor",
  "ID": "420", 
  "Name": "Olympics"     
}

因为LS_ID =" 420"有两种问题类型。

我尝试在 locationData 上迭代选定的值,以便在比较中获得 LS_ID ,但是,我得到了已选择的所有LS-ID'值(' OR'条件)而不是获取所有选定值**的LS_ID(' AND'条件)**

export function filterWithIssueSelectedValue(mainData,selections,locationData){
  let siteIds=[], filteredSSData=[]
  locationData.map((row,index)=>{
    selections.map((issue)=>{
      if(_.includes(row["Problem Type"], issue)){
        siteIds.push(row.LS_ID)
     }
  })     
})

 siteIds=_.uniq(siteIds);
 if(siteIds && siteIds.length){
    mainData.map((row,index)=>{
        siteIds.map((id)=>{
                if(row.ID==id){
                    filteredSSData.push(row)
                }
        })
    })
}

filteredSSData=_.uniqBy(filteredSSData, 'ID')
return filteredSSData
}

有人可以帮助我进行此过滤。谢谢in-Advance !!

2 个答案:

答案 0 :(得分:2)

这应该这样做:

let locationData= [
  {
    "Name": "Olympics",
    "Problem Type": "Access",
    "LS_ID": "421"
  },
  {
    "Name": "Olympics",
    "Problem Type": "Route Issue",
    "LS_ID": "420"
  },
  {
    "Name": "Olympics",
    "Problem Type": "Tools",
    "LS_ID": "420"
  },
  {
    "Name": "Olympics",
    "Problem Type": "Access",
    "LS_ID": "420"
  },
  {
    "Name": "Olympics",
    "Problem Type": "Tools",
    "LS_ID": "422"
  }
]

let mainData= [
  {
    "SiteType": "Outdoor",
    "ID": "421", 
    "Name": "Olympics"     
  },
  {
    "SiteType": "Indoor",
    "ID": "420", 
    "Name": "Olympics"     
  },
  {
    "SiteType": "International",
    "ID": "422", 
    "Name": "Olympics"    
  },
  {
    "SiteType": "Local",
    "ID": "423",  
    "Name": "Olympics"    
  }
]

const findSiteThatHasProblems = (problems = []) =>
  mainData.filter(location =>
    problems.every(problem => 
      !!locationData.find(locationInfo =>
        locationInfo.LS_ID === location.ID && locationInfo['Problem Type'] === problem
      )
    )
  )

console.log(findSiteThatHasProblems(['Tools', 'Access']))

如果您希望我为此代码添加任何解释,请与我们联系!

答案 1 :(得分:0)

我建议首先过滤ID并将结果用于过滤mainData

var locationData = [{ Name: "Olympics", "Problem Type": "Access", LS_ID: "421" }, { Name: "Olympics", "Problem Type": "Route Issue", LS_ID: "420" }, { Name: "Olympics", "Problem Type": "Tools", LS_ID: "420" }, { Name: "Olympics", "Problem Type": "Access", LS_ID: "420" }, { Name: "Olympics", "Problem Type": "Tools", LS_ID: "422" }],
    mainData = [{ SiteType: "Outdoor", ID: "421", Name: "Olympics" }, { SiteType: "Indoor", ID: "420", Name: "Olympics" }, { SiteType: "International", ID: "422", Name: "Olympics" }, { SiteType: "Local", ID: "423", Name: "Olympics" }],
    selections = ['Access', 'Tools'],
    ids = Array.from(
        locationData
            .filter(({ 'Problem Type': type }) => selections.includes(type))
            .reduce((m, { LS_ID }) => m.set(LS_ID, (m.get(LS_ID) || 0) + 1), new Map)
            .entries()
        )
        .filter(({ 1: count }) => count === selections.length)
        .map(({ 0: id }) => id),
    result = mainData.filter(({ ID }) => ids.includes(ID));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }