[英]Find value in multidimensional array. 3 levels deep
I have an array more or less like this 我或多或少有这样的数组
questionnaires[
{
'id': 14,
question_asked_groups: [
{
'id': 11,
questions_asked: [
{
'id': 10,
'list_id': 147,
'answer': "A lot of the time"
},
{
'id': '11',
'list_id': 148,
'answer': "Quite often"
},
]
},
{
'id': 22,
questions_asked: [
{
'id': 13,
'list_id': 188,
'answer': "Never"
},
{
'id': '14',
'list_id': 190,
'answer': "Yesterday"
},
]
},
]
},
]
I need to set the correct button (answer) as active. 我需要将正确的按钮(答案)设置为活动状态。 The array is a collection of all answered questions structured in a array as
数组是数组中所有已回答问题的集合,结构如下
questionnaire
-> question_asked_groups
-> questions_asked
questionnaire
-> question_asked_groups
-> questions_asked
So while loading the screen with all the buttons, when any button is loaded I want to take its id ( 147 for example ) and search for it in the questionnaires
array and return the answer
因此,在使用所有按钮加载屏幕时,当加载任何按钮时,我想获取其ID(例如147)并在
questionnaires
数组中搜索它并返回answer
Hope that makes sense. 希望有道理。 I will expand the question if you have questions.
如果您有任何问题,我将扩大问题。
I have tried a implementation of something I though was similar. 我尝试过实现与我相似的东西。
_find(array, list_id) {
let object;
array.some(function f(a) {
if (a.list_id === list_id) {
object = a;
return true;
}
if (Array.isArray(a.items)) {
return a.items.some(f);
}
});
return object;
}
But cant make it work. 但是不能使其工作。
PS. PS。 Not sure if there is a lodash solutions for this type of thing.
不知道这种事情是否有lodash解决方案。 I have seen solutions like this but fails aswell.
我曾见过这样的解决方案这样 ,但藏汉失败。
I'd first map
to an array of arrays of the question_asked_groups
, and then flatten with .flat
- then, you can find the right object with .find
: 我首先
map
到一个question_asked_groups
数组的数组,然后使用.flat
展平-然后,您可以使用.find
找到合适的对象:
const questionnaires=[{id:14,question_asked_groups:[{id:11,questions_asked:[{id:10,list_id:147,answer:"A lot of the time"},{id:"11",list_id:148,answer:"Quite often"}]},{id:22,questions_asked:[{id:13,list_id:188,answer:"Never"},{id:"14",list_id:190,answer:"Yesterday"}]}]}]; const allQuestionsAsked = questionnaires.map( ({ question_asked_groups }) => question_asked_groups.map( ({ questions_asked }) => questions_asked ) ).flat(2); console.log( allQuestionsAsked.find(({ list_id }) => list_id === 147).answer );
For older browsers, either include a polyfill, or use a different method: 对于较旧的浏览器,要么包含一个polyfill,要么使用其他方法:
const questionnaires=[{id:14,question_asked_groups:[{id:11,questions_asked:[{id:10,list_id:147,answer:"A lot of the time"},{id:"11",list_id:148,answer:"Quite often"}]},{id:22,questions_asked:[{id:13,list_id:188,answer:"Never"},{id:"14",list_id:190,answer:"Yesterday"}]}]}]; const allQuestionsAsked = []; questionnaires.forEach( ({ question_asked_groups }) => question_asked_groups.forEach( ({ questions_asked }) => allQuestionsAsked.push(...questions_asked) ) ); console.log( allQuestionsAsked.find(({ list_id }) => list_id === 147).answer );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.