[英]Javascript filter array of object base on the text inside of array of objects
I have the following structure: 我有以下结构:
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"
}
]
}
]
and I trying to filter the items of the structure with the following code: 并且我尝试使用以下代码过滤结构的项目:
const term = RegExp('Browsers')
structure.filter(item =>
item.questions.find(question => term.test(question.title)))
and I getting: 我得到:
{
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"}
}
but I need: 但是我需要:
{
item_type: "questionnaire",
questionnaire_type: "system_information",
questions:[
{id: "1e24c388-5e65-e711-8358-000c29a887ad", type: "one_answer", title: "Browsers"}
}
how can I get only what I need 我怎样才能只得到我所需要的
Firstly you should select the items from the structure which has question with the desired title, secondly, you have to remove every other questions 首先,您应该从结构中选择需要标题的问题;其次,您必须删除所有其他问题
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))
}))
You should switch your filter and find function. 您应该切换过滤器和查找功能。 The actual request is asking for elements in the array that have a child containing this RegEx on questions, that is why you get a single element with all the 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"}
}
Instead you should tell him to find parent items with filtered questions containing Browser, you can try this: 相反,您应该告诉他查找包含浏览器的已过滤问题的父项,您可以尝试以下操作:
const term = new RegExp('Browsers', i);
structure.find(item =>
item.questions.filter(question => term.test(question.title)));
This should give you what you want 这应该给你你想要的
Old school approach: 老派做法:
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 getQuestion = (t) => { const term = RegExp(t) var result = 'not found' for (var item of structure) { var q = item.questions.find(question => term.test(question.title)) if (q) { result = Object.assign({}, item) result.questions = [q] break } } return result } console.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)
I think you are looking for this. 我想您正在寻找这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.