How to show only some javascript object data

0

I have the following code:

    // ARREGLO
producto = [
    { n: 'Jabon en Pasta', az: 1, ro: 0, am: 0, bla: 0, c: ' ', v: 0, t: 'sol', grupo: 'No Incluidas' },
    { n: 'Ambientador', az: 2, ro: 1, am: 0, bla: 0, c: ' ', v: 0, t: 'liq', grupo: 'Incluidas' },
    { n: 'Cloro', az: 2, ro: 0, am: 1, bla: 0, c: ' ', v: 0, t: 'liq', grupo: 'No Incluidas' }, 
    { n: 'Desinfectante', az: 2, ro: 1, am: 0, bla: 0, c: ' ', v: 0, t: 'liq', grupo: 'Incluidas' } ];

I want to obtain a separate listing of group products included and not included I've tried something like:

ncompoa = "<h1>Formulas Incluidas</h1><ol>";
     for (li in producto) {
         ncompoa += "<li>";
         if (producto.grupo = 'Incluidas') {
             ncompoa +=  producto[li].n;
         }
        ncompoa += "</li>";
        };
        ncompoa += "</ol>";
    document.getElementById('contenido').innerHTML = ncompoa;

But the result obtained is complete of all products included and not included.

    
asked by Jose M Herrera V 06.08.2018 в 00:47
source

1 answer

1

The object you are trying to query in an arrayObjet, for which you will have to first check its position [i] then its property.

I also found this error in if (product.group = 'Included'), apparently you forgot one = the correct thing is if (product.group == 'Included'). I hope you help.

producto = [
    { n: 'Jabon en Pasta', az: 1, ro: 0, am: 0, bla: 0, c: ' ', v: 0, t: 'sol', grupo: 'No Incluidas' },
    { n: 'Ambientador', az: 2, ro: 1, am: 0, bla: 0, c: ' ', v: 0, t: 'liq', grupo: 'Incluidas' },
    { n: 'Cloro', az: 2, ro: 0, am: 1, bla: 0, c: ' ', v: 0, t: 'liq', grupo: 'No Incluidas' }, 
    { n: 'Desinfectante', az: 2, ro: 1, am: 0, bla: 0, c: ' ', v: 0, t: 'liq', grupo: 'Incluidas' } ];
    
     ncompoa = "<h1>Formulas Incluidas</h1><ol>";
     ncompoa2 = "<h1>Formulas No Incluidas</h1><ol>";
     for (li in producto) {         
         if (producto[li].grupo == "Incluidas") {
         	ncompoa += "<li>";
            ncompoa +=  producto[li].n;
            ncompoa += "</li>";
         }
         else{
         	ncompoa2 += "<li>";
            ncompoa2 +=  producto[li].n;
            ncompoa2 += "</li>";
         } 
     }
     ncompoa += "</ol>";
     ncompoa2 += "</ol>";
     document.getElementById('contenido').innerHTML = ncompoa;
     document.getElementById('contenido2').innerHTML = ncompoa2;
<!DOCTYPE html>
<html>
<body>

<p id="contenido"></p>
<p id="contenido2"></p>

</body>
</html>
    
answered by 06.08.2018 / 01:20
source