简体   繁体   中英

How to loop through child elements and create nested array of objects?

I have nested checkboxes inside a <ul> / <li> list which can have as parent checkbox with .check-negozio class. Inside that parent there could be some checkboxes with .check-cassa class and those could contain other children checkboxes signed with .check-operatore

So html structure is the following and there could be various <li> with .check-negozio as parent

Now i need to build an object that will be as the following

[{ negozio: npv, cassa: [{ cassa: cs, operatore: [] }] }] 

so from the example below, the object must look like this

[{ negozio: 0, cassa: [{ cassa: 1, operatore: [] }, { cassa: 5, operatore: [0]}] }] 

To achieve this, I must loop through each .check-negozio then inside same loop I have to loop through that .check-negozio children .check-cassa and if the .check-cassa has children .check-operator loop through them.

I was trying to do the following but $(this).children(".check-cassa").each seems to be the wrong approach as it doesn't even enter inside that loop... how should I loop through nested elements like this?

 $(".check-negozio").each(function() { if (this.checked || this.indeterminate) { // if parent checkbox is checked add it to object const npv = $(this).attr('data-npv'); selezione.negozio = npv; $(this).children(".check-cassa").each(function() { // here i should loop throw children .check-cassa of .check-negozio if (this.checked || this.indeterminate) { // if check-cassa is checked go ahead const cs = $(this).attr('data-cs'); if ($(this).closest('li').has('ul').length) { // here i check if check-cassa contain any child (in this case check-operatore) $(this).children(".check-operatore").each(function() { // if there are child check-operatore loop throw them if (this.checked) { // if checked add data-op inside array of operatore const op = $(this).attr('data-op'); operatore.push({ OP: op }); } }) selezione.cassa.push({ CS: cs, OP: operatore }) // here i add data-cs and array operatore inside object } else { // else if there isn't any child check-operatore just add data-cs with empty array of operatore selezione.cassa.push({ CS: cs, OP: [] }); } } }) config.push(selezione); // adding created object inside array } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li> <input type="checkbox" class="check-negozio" data-npv="0"> <ul> <li class="nav-item"> <input type="checkbox" class="check-cassa" data-cs="1"> </li> <li> <input type="checkbox" class="check-cassa" data-cs="5"> <ul> <li> <input type="checkbox" class="check-operatore" data-op="0"> </li> </ul> </li> </ul> </li> </ul>

Actually the wrong approach was as @vqf said in comments is to loop throw .check-cassa by using .check-negozio as parent when i had to get firstly the parent of .check-negozio and then loop throw it's children .check-cassa i used the same approach where i was trying to loop throw .check-operatore firstly i get parent of .check-cassa and then looped throw it's children .check-operatore then i made some adjustment for get the right array of object by initializing object and arrays for check-casse and check-operatori inside the parent loop

here is the working code

var config = [];


$(".check-negozio").each(function () {
    var selezione = {};
    var cassa = [];
    var operatore = [];
    if (this.checked || this.indeterminate) { // if parent checkbox is checked add it to object

        const npv = $(this).attr('data-npv');
        selezione.NPV = npv;

        $(".check-cassa", $(this).parent()).each(function () { // here i should loop throw children .check-cassa of .check-negozio
            if (this.checked || this.indeterminate) { // if check-cassa is checked go ahead
                const cs = $(this).attr('data-cs');

                if ($(this).closest('li').has('ul').length) { // here i check if check-cassa contain any child (in this case check-operatore)

                    $(".check-operatore", $(this).parent()).each(function () { // if there are child check-operatore loop throw them
                        if (this.checked) { // if checked add data-op inside array of operatore
                            const op = $(this).attr('data-op');
                            operatore.push({ OP: op });
                        }
                    })
                    cassa.push({ CS: cs, OP: operatore }) // here i add data-cs and array operatore inside object
                } else { // else if there isn't any child check-operatore just add data-cs with empty array of operatore
                    cassa.push({ CS: cs, OP: [] });
                }
                selezione.cassa = cassa;
            }
        })
        config.push(selezione); // adding created object inside array
    }
})

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