TransWikia.com

How to filter JSON Data in JS

Stack Overflow Asked by user12129876 on December 25, 2021

I have a of JSON Data of jobs where each job contain the following information

{
  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",
    },
  ]
}

I want to filter the JSON using preferred_location variable

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

Example : If user selects solapur ( Maharashtra ) and district (Solapur)

The list should return only those jobs which contain this values in the preferred_location

I tried filtereing using job description like this

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)
     );
  });
}

I want to do the same for preferred_location variable but not able to do so.

One Answer

preferred_location is an array inside an array of jobs, so you need a double filter

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
})

Answered by gbalduzzi on December 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP