Jean
Jean

Reputation: 5411

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

Upvotes: 0

Views: 52

Answers (4)

Omprakash Sharma
Omprakash Sharma

Reputation: 439

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.

Upvotes: 0

Kosh
Kosh

Reputation: 18413

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

Upvotes: 0

Joan Miquel
Joan Miquel

Reputation: 286

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

Upvotes: 0

ltamajs
ltamajs

Reputation: 1341

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

Upvotes: 1

Related Questions