简体   繁体   中英

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

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

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. Not sure if there is a lodash solutions for this type of thing. 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 :

 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM