基于对象数组内部文本的Javascript对象过滤器数组
我的结构如下:基于对象数组内部文本的Javascript对象过滤器数组,javascript,Javascript,我的结构如下: const structure = [ { "item_type": "questionnaire", "questionnaire_type": "profile", "questions": [ { "id": "a123c388-5e65-e711-8358-000c29a887ad", "type": "one_answer",
const structure = [
{
"item_type": "questionnaire",
"questionnaire_type": "profile",
"questions": [
{
"id": "a123c388-5e65-e711-8358-000c29a887ad",
"type": "one_answer",
"title": "Participant segment"
}
]
},
{
"item_type": "questionnaire",
"questionnaire_type": "system_information",
"questions": [
{
"id": "0624c388-5e65-e711-8358-000c29a887ad",
"type": "one_answer",
"title": "Operating System"
},
{
"id": "1e24c388-5e65-e711-8358-000c29a887ad",
"type": "one_answer",
"title": "Browsers"
},
{
"id": "5224c388-5e65-e711-8358-000c29a887ad",
"type": "one_answer",
"title": "Screen Resolution"
},
{
"id": "8524c388-5e65-e711-8358-000c29a887ad",
"type": "one_answer",
"title": "Browser Resolution"
}
]
},
{
"item_type": "questionnaire",
"questionnaire_type": "final_questionnaire",
"questions": [
{
"id": "0326c388-5e65-e711-8358-000c29a887ad",
"type": "one_answer",
"title": "af"
}
]
}
]
我尝试用以下代码过滤结构的项:
const term = RegExp('Browsers')
structure.filter(item =>
item.questions.find(question => term.test(question.title)))
我得到:
{
item_type: "questionnaire",
questionnaire_type: "system_information",
questions:[
{id: "0624c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Operating System"},
{id: "1e24c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Browsers"},
{id: "5224c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Screen Resolution"}3: {id: "8524c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Browser Resolution"}
}
但我需要:
{
item_type: "questionnaire",
questionnaire_type: "system_information",
questions:[
{id: "1e24c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Browsers"}
}
我怎样才能只得到我需要的东西呢?首先,你应该从结构中选择带有所需标题的问题,其次,你必须删除所有其他问题
structure
.filter(item =>
item.questions.some(question => term.test(question.title)))
.map(item => Object.assign({}, item, {
questions: item.questions.filter(question => term.test(question.title))
}))
您应该切换过滤器并查找函数。实际请求要求数组中的元素有一个子元素包含此RegEx on questions,这就是为什么得到一个包含所有问题的元素:
{
item_type: "questionnaire",
questionnaire_type: "system_information",
questions:[
{id: "0624c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Operating System"},
{id: "1e24c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Browsers"},
{id: "5224c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Screen Resolution"}3: {id: "8524c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Browser Resolution"}
}
相反,您应该告诉他查找包含浏览器的筛选问题的父项,您可以尝试以下操作:
const term = new RegExp('Browsers', i);
structure.find(item =>
item.questions.filter(question => term.test(question.title)));
这应该给你你想要的老式方法:
const结构=[{
“项目类型”:“调查问卷”,
“调查问卷类型”:“个人资料”,
“问题”:[{
“id”:“a123c388-5e65-e711-8358-000c29a887ad”,
“类型”:“一个答案”,
“标题”:“参与者部分”
}]
},
{
“项目类型”:“调查问卷”,
“调查问卷类型”:“系统信息”,
“问题”:[{
“id”:“0624c388-5e65-e711-8358-000c29a887ad”,
“类型”:“一个答案”,
“标题”:“操作系统”
},
{
“id”:“1e24c388-5e65-e711-8358-000c29a887ad”,
“类型”:“一个答案”,
“标题”:“浏览器”
},
{
“id”:“5224c388-5e65-e711-8358-000c29a887ad”,
“类型”:“一个答案”,
“标题”:“屏幕分辨率”
},
{
“id”:“8524c388-5e65-e711-8358-000c29a887ad”,
“类型”:“一个答案”,
“标题”:“浏览器分辨率”
}
]
},
{
“项目类型”:“调查问卷”,
“调查问卷类型”:“最终调查问卷”,
“问题”:[{
“id”:“0326c388-5e65-e711-8358-000c29a887ad”,
“类型”:“一个答案”,
“标题”:“af”
}]
}
]
const getQuestion=(t)=>{
常数项=RegExp(t)
变量结果='未找到'
对于(结构的var项){
VarQ=项目.问题.查找(问题=>术语.测试(问题.标题))
if(q){
结果=对象。分配({},项)
result.questions=[q]
打破
}
}
返回结果
}
log(getQuestion('Browsers'))
console.log(getQuestion('Heck'))
我想你在找这个
var filteredStructure = structure.map(sto =>{
return sto.questions.filter(ql=>{
//return ql.title.match(term);
return term.test(ql.title);
});
});
console.log(filteredStructure)