繁体   English   中英

基于对象数组内部文本的Javascript过滤器数组

[英]Javascript filter array of object base on the text inside of array of objects

我有以下结构:

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

您应该切换过​​滤器和查找功能。 实际的请求是在数组中查询具有包含正则表达式的子元素的元素,这就是为什么您在所有问题中得到一个元素的原因:

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

我想您正在寻找这个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM