【问题标题】:How to filter JSON Data in JS如何在 JS 中过滤 JSON 数据
【发布时间】:2020-11-14 02:48:52
【问题描述】:

我有一份工作的 JSON 数据,其中每个工作都包含以下信息

{
  jobs: [
    {
      jobseeker_create_job_id: 1,
      job_title: "Fashion Designer",
      job_description: "Fashion Designer",
      salary: "{'value': 2, 'label': 'Rs.10,001/- to Rs12,000/-'}",
      preferred_location: "[]",
      trade: "",
      trade_cat: "",
      perks: "[]",
      pia_id: 5,
      valid_till: "2020-07-22",
      note: "none",
      CreatedDate: "2020-07-22T15:49:32.120875+05:30",
      ModifiedBy: null,
      ModifiedDate: null,
      DeletedBy: null,
      DeletedDate: null,
      pia_name: "BPO Organisation",
    },
    {
      jobseeker_create_job_id: 2,
      job_title: "Fashion Designer Intern",
      job_description: "Fashion Designer Intern",
      salary: "{'value': 2, 'label': 'Rs.10,001/- to Rs12,000/-'}",
      preferred_location:
        "[{'state': {'value': 22, 'label': 'Maharashtra'}, 'district': {'value': 2821, 'label': 'Solapur'}}]",
      trade: "domain",
      trade_cat: "{'value': 17, 'label': 'FASHION DESIGN'}",
      perks: "[]",
      pia_id: 0,
      valid_till: "2020-07-22",
      note: "no",
      CreatedDate: "2020-07-22T15:51:37.256110+05:30",
      ModifiedBy: null,
      ModifiedDate: null,
      DeletedBy: null,
      DeletedDate: null,
      pia_name: "Admin",
    },
    {
      jobseeker_create_job_id: 4,
      job_title: "Test experience",
      job_description: "Test experience",
      salary: "{'value': 2, 'label': 'Rs.10,001/- to Rs12,000/-'}",
      preferred_location:
        "[{'state': {'value': 22, 'label': 'Maharashtra'}, 'district': {'value': 2821, 'label': 'Solapur'}}]",
      trade: "",
      trade_cat: "",
      perks: "[]",
      pia_id: 5,
      valid_till: "2020-07-23",
      note: "none",
      CreatedDate: "2020-07-23T12:35:01.708645+05:30",
      ModifiedBy: null,
      ModifiedDate: null,
      DeletedBy: null,
      DeletedDate: null,
      pia_name: "BPO Organisation",
    },
    {
      jobseeker_create_job_id: 5,
      job_title: "Test2 Update",
      job_description: "asd",
      salary: "{'value': 3, 'label': 'Rs.12,001/- to Rs.15000/-'}",
      preferred_location:
        "[{'state': {'value': 22, 'label': 'Maharashtra'}, 'district': {'value': 2821, 'label': 'Solapur'}}, {'state': {'value': 22, 'label': 'Maharashtra'}, 'district': {'value': 2763, 'label': 'Pune'}}]",
      trade: "",
      trade_cat: "",
      perks: "[]",
      pia_id: 5,
      is_admin: false,
      is_active: true,
      is_approve: true,
      valid_till: "2020-07-23",
      note: "none",
      CreatedDate: "2020-07-23T13:11:48.132243+05:30",
      ModifiedBy: null,
      ModifiedDate: null,
      DeletedBy: null,
      DeletedDate: null,
      pia_name: "BPO Organisation",
    },
  ]
}

我想使用preferred_location 变量过滤JSON

preferred_location: "[{'state': {'value': 22, 'label': 'Maharashtra'}, 'district': {'value': 2821, 'label': 'Solapur'}}, {'state': {'value': 22, 'label': 'Maharashtra'}, 'district': {'value': 2763, 'label': 'Pune'}}]",

示例:如果用户选择 solapur ( Maharashtra ) 和 District (Solapur)

列表应该只返回那些在preferred_location中包含这个值的工作

我尝试使用这样的职位描述进行过滤

if (values.designation != null) {
//selected will hold the input values which user will enter
let selected = [].slice.call(values.designation).map((o) => {
return o.value;
});

// list contains JSON Data of jobs 
list = list.filter((e) => {
let designation_json = e.designation.replace(/'/g, '"');
let designation_json_decoded = JSON.parse(designation_json);
return selected.includes(
parseInt(designation_json_decoded.value)
     );
  });
}

我想对 preferred_location 变量做同样的事情,但不能这样做。

【问题讨论】:

    标签: javascript json reactjs filter


    【解决方案1】:

    preferred_location 是作业数组中的一个数组,因此您需要一个双重过滤器

    const targetState = 'Maharashtra'
    const targetDistrict = 'Solapur'
    
    const jobs = [ ... your jobs array...]
    
    const filteredJobs = jobs.filter((job) => {
      const locations = JSON.parse(job.preferred_location)
      const filteredLocations = locations.filter(location => {
        return location.state.label === targetState && location.district.label === targetDistrict
      })
      
      return filteredLocations.length > 0
    })
    

    【讨论】:

    • 其实州和区可以包含多个值。那么在这种情况下我该怎么办?我实际上需要一个单独的过滤器用于州和地区
    • 目标州和目标区可以是多个吗?已经处理了同一个作业中有多个位置的情况
    • 是的,目标州和目标区可以是多个。我可以向你解释更多。在前端有一个州和地区的多选。如果用户选择马哈拉施特拉邦,让我们假设 Solpaur 区。他应该只看到那些与任何特定工作的首选位置变量匹配的工作。
    • 您应该编辑第二个filter 函数的内容以检查多个值,如果您知道变量是如何构建的,这应该不难。
    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多