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

时间:2018-02-19 17:45:09

标签: javascript

我有以下结构:

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

我怎样才能得到我需要的东西

4 个答案:

答案 0 :(得分:1)

首先,您应该从结构中选择具有所需标题的问题的项目,其次,您必须删除所有其他问题

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

答案 1 :(得分:0)

您应该切换过​​滤器并查找功能。实际的请求是要求数组中的元素包含一个包含此RegEx的子问题,这就是为什么你得到一个包含所有问题的元素的原因:

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

这可以给你你想要的东西

答案 2 :(得分:0)

旧学校方法:

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

答案 3 :(得分:0)

var filteredStructure = structure.map(sto =>{
  return sto.questions.filter(ql=>{
    //return ql.title.match(term);
    return term.test(ql.title);
  });
});
console.log(filteredStructure)

我认为你正在寻找这个。