Skip to content
Advertisement

How Can I Make a Loop of two arrays with some rules

I need some help

I have two arrays:

First array have Boolean elements. (It’s true when button is active , and false if not)
The second one strings where “0”(active) or “1” (inactive).

let arrayBotoesCategorias = [iconCategoriaIndividual, iconCategoriaGrupo, iconCategoriaIndoor, iconCategoriaOutdoor, iconCategoriaAquatico,iconCategoriaNatureza];
let arrayCategorias = [item.individual, item.grupo, item.indoor, item.outdoor, item.aquatico, item.natureza];

To understand better , PaintBall have grupo(group), indoor, outdoor and natureza(nature) activated. The rest are inactive. Like this

individual: "0",
grupo: "1",
indoor: "1",
outdoor: "1",
aquatico: "0",
natureza: "1",

What I need it’s the following but of course with another solution:

if(arrayBotoesCategorias[1] == true && arrayBotoesCategorias[2] == true && arrayBotoesCategorias[3] == true && arrayBotoesCategorias[5] == true){
    if(arrayCategorias[1] == "1" && arrayCategorias[2] == "1" && arrayCategorias[3] == "1" && arrayCategorias[5] == "1"){
        return(itemSection)
    }
}

When a element from the first array is true and the same position of the second is == “1” , return something.

Example

In this example image , I Want to show the images that have those 3 tables (outdoor, aquatico and natureza with value “1”);

Advertisement

Answer

Already solved Thanks for the help!

var controlCategorias = 0;
var errorControlCategorias = 0;

            for(var i = 0; i<arrayBotoesCategorias.length; i++){
                switch (i){
                    case 0:
                        if((arrayBotoesCategorias[0] && item.individual == "1") || (!arrayBotoesCategorias[0] && item.individual == "0") || (!arrayBotoesCategorias[0] && item.individual == "1"))                        {
                            controlCategorias++;
                        }else{
                            errorControlCategorias++;
                        }
                        break;
                    case 1:
                        if((arrayBotoesCategorias[1] && item.grupo == "1") || (!arrayBotoesCategorias[1] && item.grupo == "0") || (!arrayBotoesCategorias[1] && item.grupo == "1"))                        {
                            controlCategorias++;
                        }else{
                            errorControlCategorias++;
                        }
                        break;
                    case 2:
                        if((arrayBotoesCategorias[2] && item.indoor == "1") || (!arrayBotoesCategorias[2] && item.indoor == "0") || (!arrayBotoesCategorias[2] && item.indoor == "1"))                        {
                            controlCategorias++;
                        }else{
                            errorControlCategorias++;
                        }
                        break;
                    case 3:
                        if((arrayBotoesCategorias[3] && item.outdoor == "1") || (!arrayBotoesCategorias[3] && item.outdoor == "0") || (!arrayBotoesCategorias[3] && item.outdoor == "1"))                        {
                            controlCategorias++;
                        }else{
                            errorControlCategorias++;
                        }
                        break;
                    case 4:
                        if((arrayBotoesCategorias[4] && item.aquatico == "1") || (!arrayBotoesCategorias[4] && item.aquatico == "0") || (!arrayBotoesCategorias[4] && item.aquatico == "1"))                        {
                            controlCategorias++;
                        }else{
                            errorControlCategorias++;
                        }
                        break;
                    case 5:
                        if((arrayBotoesCategorias[5] && item.natureza == "1") || (!arrayBotoesCategorias[5] && item.natureza == "0") || (!arrayBotoesCategorias[5] && item.natureza == "1"))                        {
                            controlCategorias++;
                        }else{
                            errorControlCategorias++;
                        }
                        break;
                }
                    
            }

            if(controlCategorias > 0 && errorControlCategorias == 0){
                return itemSection;
            }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement