I have this situation. Two arrays, Questions and Options. Is there any way to create a new array, with a new item Options with all options that contains id_question = 1 with Javascript? I tried with find() but it returns only one option, not four. Examples below:
const questions = [{
"id": 1,
"image": "96335de36bec-viagem1.png",
"title": "Quantas viagens você costuma fazer por ano?"
},
{
"id": 2,
"image": "b3f2d639f6fd-aviao2.png",
"title": "Qual foi o destino de sua última viagem?"
},
{
"id": 3,
"image": "53125381aec5-praia1.png",
"title": "Qual tipo de clima você deseja para sua próxima viagem?"
},
{
"id": 4,
"image": "1a9c51a4dd84-aviaopessoa.png",
"title": "Você planeja viajar para fora do Brasil?"
}
]
const options = [{
id: 1,
id_question: 1,
value: '1',
description: 'Uma vez ao ano'
},
{
id: 2,
id_question: 1,
value: '3',
description: 'Até 3 vezes ao ano'
},
{
id: 3,
id_question: 1,
value: '6',
description: 'Até 6 vezes ao ano'
},
{
id: 4,
id_question: 1,
value: '12',
description: 'Pelo menos uma vez a cada mês'
},
{
id: 5,
id_question: 2,
value: 'NORTE_NORDESTE',
description: 'Região Norte/Nordeste do Brasil'
},
{
id: 6,
id_question: 2,
value: 'SUL_SUDESTE',
description: 'Região Sul/Sudeste do Brasil'
},
{
id: 7,
id_question: 2,
value: 'CENTRO',
description: 'Região Central do Brasil'
},
{
id: 8,
id_question: 2,
value: 'VIAJOU_FORA',
description: 'Para fora do Brasil'
}
]
I want the new Array with the objects like this
{
"question": {
"id": 1,
"image": "96335de36bec-viagem1.png",
"title": "Quantas viagens você costuma fazer por ano?",
},
"options": [{
"id": 1,
"id_question": 1,
"value": "1",
"description": "Uma vez ao ano"
},
{
"id": 2,
"id_question": 1,
"value": "3",
"description": "Até 3 vezes ao ano"
},
{
"id": 3,
"id_question": 1,
"value": "6",
"description": "Até 6 vezes ao ano"
},
{
"id": 4,
"id_question": 1,
"value": "12",
"description": "Pelo menos uma vez a cada mês"
}
]
}