Tour matrix with jquery

1

I have this array of arrays, to show a use value:

alert(var_dump(objView.ArrayStock[1][0].STOCK_REAL));

ArrayStock:[[{BODEGA: "01", STOCK_REAL: "17.00"}, {BODEGA: "03", STOCK_REAL: "9.00"}],…]
0:[{BODEGA: "01", STOCK_REAL: "17.00"}, {BODEGA: "03", STOCK_REAL: "9.00"}]
    0:{BODEGA: "01", STOCK_REAL: "17.00"}
    1:{BODEGA: "03", STOCK_REAL: "9.00"}
1:[{BODEGA: "01", STOCK_REAL: "17.00"}, {BODEGA: "03", STOCK_REAL: "9.00"}]
    0:{BODEGA: "01", STOCK_REAL: "17.00"}
    1:{BODEGA: "03", STOCK_REAL: "9.00"}
2:[{BODEGA: "01", STOCK_REAL: "17.00"}, {BODEGA: "03", STOCK_REAL: "9.00"}]
    0:{BODEGA: "01", STOCK_REAL: "17.00"}
    1:{BODEGA: "03", STOCK_REAL: "9.00"}

Currently for normal arrays use:

$.each(objView.ArrayStock, function (indice, elemento) {
alert(indice + " : " + elemento.STOCK_REAL);/* no se como recorrerlo cuando es un array de arrays*/
});

In what way would I have to go to show only the "STOCK_REAL" of the 3 arrays ?, I imagine that I have to use two each or there will be a simpler way to traverse it.

This is what I carry, I can show only one element:

$.each(objView.ArrayStock, function (indice, elemento) {
    alert(indice + " : " + elemento[indice]);

    each(elemento,function(indice2, elemento2) {
      alert(indice + " : " + elemento2[indice2].STOCK_REAL);
    });
});
    
asked by Javier Antonio Aguayo Aguilar 19.05.2017 в 19:12
source

2 answers

2

To go through a matrix , you just have to add another cycle, one cycle runs vertically and another cycle horizontally, when it is only one cycle it is done horizontally

A matrix is a array which in each index has another array , what you currently do is traverse only one array, but as in this array in the indexes you have another arrangement, you have to go through this second arrangement with another cycle.

I leave an example with your matrix:

var objView = {ArrayStock: [
  [{
    BODEGA: "01",
    STOCK_REAL: "17.00"
  }, {
    BODEGA: "03",
    STOCK_REAL: "9.00"
  }],
  [{
    BODEGA: "01",
    STOCK_REAL: "17.00"
  }, {
    BODEGA: "03",
    STOCK_REAL: "9.00"
  }],
  [{
    BODEGA: "01",
    STOCK_REAL: "17.00"
  }, {
    BODEGA: "03",
    STOCK_REAL: "9.00"
  }]
]}

$.each(objView.ArrayStock, function(indice, elemento) {
  $.each(elemento, function(indice2, elemento2) {
    console.log(indice + " : " + elemento2.STOCK_REAL);
    /* no se como recorrerlo cuando es un array de arrays*/
  })

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 19.05.2017 / 19:20
source
0

here another example with recursion

var data = [
  [{
    BODEGA: "01",
    STOCK_REAL: "17.00"
  }, {
    BODEGA: "03",
    STOCK_REAL: "9.00"
  }],
  [{
    BODEGA: "01",
    STOCK_REAL: "17.00"
  }, {
    BODEGA: "03",
    STOCK_REAL: "9.00"
  }],
  [{
    BODEGA: "01",
    STOCK_REAL: "17.00"
  }, {
    BODEGA: "03",
    STOCK_REAL: "9.00"
  }]
]
recorrerBodegas(data);
function recorrerBodegas(data){
  for (detalle in data){

   if(Array.isArray(data[detalle])){
    recorrerBodegas(data[detalle]);
   }else{
        console.log("para el detalle " + detalle + " el resultado de BODEGA:"+ data[detalle].BODEGA);
        console.log("para el detalle " + detalle + " el resultado de STOCK_REAL:"+ data[detalle].STOCK_REAL);
   }
  }
}

the example in link

    
answered by 19.05.2017 в 19:36