【发布时间】: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