简体   繁体   English

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

[英]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.

相关问题 如何在对象数组 Javascript 中过滤数组 object? - How do I filter an array object inside of a array of objects Javascript? 在javascript中过滤数组内对象内的数组 - Filter an array inside an object inside an array in javascript 按标记过滤对象(对象数组内部的数组) - Filter object by tag (array inside an array of objects) 使用单个 object 内的数组过滤对象数组 - Filter an array of objects with an array inside a single object 在 JavaScript 中的对象数组中过滤对象上的数组 - Filter on arrays on Objects inside array of Objects in JavaScript 在对象数组中过滤 object 内的 object 内的数组 - Filter an array inside an object inside an object inside in array of objects 通过迭代对象内的数组中包含的对象内的属性过滤对象数组[Javascript] - Filter an array of objects by the property inside an object contained in an array inside the object iterated [Javascript] 如何通过 Javascript 中的过滤器 object 过滤对象数组? - How to filter in an array of objects by filter object in Javascript? 根据javascript中对象数组内的值过滤对象数组 - Filter an array of objects upon a value inside an array of objects inside in javascript Javascript 过滤数组中的对象并返回数组中 Object 中 Object 的属性 - Javascript Filter Objects in Array and return Property of Object in Array of Object in Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM