Problem : Dynamic array x index value

Asked

Viewed 32 times

0

Good people.

I have a dynamic array that receives associated data in buttons where it should index to each interaction the values (0, 1, 2, 3...). The problem is that the index value persists at zero (0) ? Follow the code :

$(document).ready(function() {
  // method click(action: atribui valores as variaveis declaradas:)

  $(".btn_data_add").click(function() {
    // method .attr(action: retorna o valor do atributo) :
    var $ws_item_in   = $(this).attr("data-item");
    var $ws_price_in  = $(this).attr("data-price");
    var $ws_icon_in   = '<a class="waves-effect waves-red btn-flat btn_data_rem"><i class="material-icons">delete_outline</i></a>';

    // estrutura colunas de uma linha de tabela html :
    var $ws_row_open  = '<tr>';
    var $ws_col_item  = '<td class="fontype-2 $ws_item_out">' + $ws_item_in + '</td>';
    var $ws_col_price = '<td class="fontype-2 $ws_price_out">' + $ws_price_in + '</td>';
    var $ws_col_icon  = '<td class="fontype-2">' + $ws_icon_in + '</td>';
    var $ws_row_close = '</tr>';

    // estrutura linha da tabela html :
    var $ws_row_in    = $ws_row_open + $ws_col_item + $ws_col_price + $ws_col_icon + $ws_row_close;
    // organiza indexando dentro de uma array :

    var $ws_array_row = [$ws_row_in];
    //
    for(var i=0; $ws_array_row.length; i++) {
      //
      console.log(i, $ws_array_row[i]); // i é o índice, matriz[i] é o valor
      break;
    }
  });
});

1 answer

0

I believe your loop execution condition is incorrect.

If you write for (var i = 0; $ws_array_row.length; i++), you’re talking to JS running your loop while $ws_array_row.length for Truthy (which looking at the code above it, would be 1, which is Truthy) he should run the code block forever.

Maybe what you want to do is this:

for (var i = 0; i < $ws_array_row.length; i++) {
  console.log(i, $ws_array_row[i]);
}
  • Didn’t work ? Continues to index the value zero (0) to variable face added !

  • The problem was that the array was inside the event . click(), needs to be declared outside and at the beginning. Thanks for the force !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.