"Kits": [ { "Reconhecimento": { "Nome": "Kit 200-300 Kwh", "Numero": 1 }, "Placas": {…
- User
"Kits": [ { "Reconhecimento": { "Nome": "Kit 200-300 Kwh", "Numero": 1 }, "Placas": { "Info": "Painel Fotovoltaico 555W - Mono - Half Cell", "Quantidade": 5 }, "Inversores": { "Info": "Inversor 3KW - Mono - 220V/60Hz", "Quantidade": 1 }, "Estrutura":{ "1":{ "Info": 1, "Quantidade": 5 }, "2":{ "Info": 1, "Quantidade":5 }, "3":{ "Info": 1, "Quantidade": 6 }, "4":{ "Info":0 , "Quantidade":0 }, "5":{ "Info": "Não Há Disponivel", "Quantidade":"Não Há Disponivel"
}, "6":{ "Info": 3, "Quantidade":14 }, "7":{ "Info": 1, "Quantidade":5 }, "8": { "Info": 2, "Quantidade": 2 }}, "Cabo":{ "Quantidade": 20 }, "Valores": { "Kwh": 300.0, "Preco": { "1": 4652.33, "2": 4907.33, "3": 4565.26, "4": 3881.23, "5": "Não Há Disponivel", "6": 4228.76, "7": 4680.40, "8": 5874.80 }, "Kwp": 2.75 }, "Imagem": { "Link": "" } } if document.getElementById("tipotelhado") == "Telha Colonial (Cerâmica)" = 1 "Telha Trapezoidal (Zinco)" = 2 "Fibrocimento (Madeira)" = 3 "Sem Suporte (Já Tenho)" = 4 - 0 "Fibrocimento (Metal)" = 5 "Telha Trapezoidal" = 6 "Laje" = 7 "Solo" = 8 if ($kit.Estrutura.1.Info == 0){ let estrutura = "" } else if (==1){ let estrutura = "Perfil de Alumínio 2.4m" } else if (==2){ let estrutura = "Kit Longarinas 4800MM - Estrutura de solo ROMAGNOLE" } else if (==3){ let estrutura = "Mini Trilho - 100mm (A) x 300mm (C) - Alto (1un)" } - Assistant
It looks like you're working on mapping roof types to specific values and then using those values to determine the appropriate structure (
estrutura) from your kit data. I'd be happy to help you correct and improve your code.Mapping Roof Types to Numbers
First, let's fix the mapping of roof types (
tipotelhado) to their corresponding numbers. Your current code has syntax errors and doesn't properly assign the numbers. Here's how you can do it:// Get the selected roof type value from the DOM let tipotelhado = document.getElementById("tipotelhado").value; // Create a mapping of roof types to numbers const tipotelhadoMapping = { "Telha Colonial (Cerâmica)": 1, "Telha Trapezoidal (Zinco)": 2, "Fibrocimento (Madeira)": 3, "Sem Suporte (Já Tenho)": 0, // Mapping to 0 as per your comment "Fibrocimento (Metal)": 5, "Telha Trapezoidal": 6, "Laje": 7, "Solo": 8 }; // Get the corresponding number for the selected roof type let tipotelhadoNumber = tipotelhadoMapping[tipotelhado];Assigning the
estruturaBased onInfoValueNext, let's correct the code that assigns the
estruturavariable based on theInfovalue from your kit data:// Assuming $kit is your kit data object let infoValue = $kit.Estrutura[tipotelhadoNumber].Info; let estrutura = ""; // Initialize the estrutura variable if (infoValue == 0) { estrutura = ""; } else if (infoValue == 1) { estrutura = "Perfil de Alumínio 2.4m"; } else if (infoValue == 2) { estrutura = "Kit Longarinas 4800MM - Estrutura de solo ROMAGNOLE"; } else if (infoValue == 3) { estrutura = "Mini Trilho - 100mm (A) x 300mm (C) - Alto (1un)"; } else { estrutura = "Não há informação disponível"; // Handle unexpected values }Putting It All Together
Here's how you can combine both pieces:
// Get the selected roof type value from the DOM let tipotelhado = document.getElementById("tipotelhado").value; // Create a mapping of roof types to numbers const tipotelhadoMapping = { "Telha Colonial (Cerâmica)": 1, "Telha Trapezoidal (Zinco)": 2, "Fibrocimento (Madeira)": 3, "Sem Suporte (Já Tenho)": 0, // Mapping to 0 as per your comment "Fibrocimento (Metal)": 5, "Telha Trapezoidal": 6, "Laje": 7, "Solo": 8 }; // Get the corresponding number for the selected roof type let tipotelhadoNumber = tipotelhadoMapping[tipotelhado]; // Access the Info value for the selected roof type number let infoValue = $kit.Estrutura[tipotelhadoNumber]?.Info; let estrutura = ""; // Initialize the estrutura variable if (infoValue == 0) { estrutura = ""; } else if (infoValue == 1) { estrutura = "Perfil de Alumínio 2.4m"; } else if (infoValue == 2) { estrutura = "Kit Longarinas 4800MM - Estrutura de solo ROMAGNOLE"; } else if (infoValue == 3) { estrutura = "Mini Trilho - 100mm (A) x 300mm (C) - Alto (1un)"; } else { estrutura = "Não há informação disponível"; } // Now you can use the 'estrutura' variable as needed console.log(`A estrutura selecionada é: ${estrutura}`);Explanation of Changes
- Mapping Roof Types: We created a
tipotelhadoMappingobject to map the roof type strings to their corresponding numbers. This approach simplifies adding or changing mappings in the future. - Adjusting for "Sem Suporte (Já Tenho)": According to your comment, "Sem Suporte (Já Tenho)" should map to
0, so we set that directly in the mapping. - Accessing Kit Data: To access the
Infovalue from your kit data, we usetipotelhadoNumberas the key in the$kit.Estruturaobject. - Nullish Coalescing Operator (
?.): We use?.when accessing$kit.Estrutura[tipotelhadoNumber]?.Infoto prevent errors in casetipotelhadoNumberis undefined or the property doesn't exist. - Conditional Statements: We've corrected the syntax and included a default case to handle unexpected
Infovalues.
Checking for Existence
It's important to ensure that the
tipotelhadoselected exists in your mapping and that the correspondingInfoexists in your kit data. You can add checks like:if (tipotelhadoNumber === undefined) { console.error("Tipo de telhado não encontrado no mapeamento."); } else if (infoValue === undefined) { console.error("Informação da estrutura não disponível no kit."); } else { // Proceed with using 'estrutura' }Conclusion
By correcting the syntax errors and restructuring your code, it should now correctly map the selected roof type to the appropriate
estrutura. If you have any further questions or need additional assistance, feel free to ask! - Mapping Roof Types: We created a