Replace each jquery

1

I need to add all the values but they present "$" and "." therefore when adding them I return a "NaN", I need to make a replace how I can integrate that into my js?

<table id="table">
              <thead>
                <tr>
                  <th>Producto</th>
                  <th>valor</th>
                </tr>
              </thead>

              <tbody>
                <tr id="1">
                  <td>Licuadora</td>
                  <td>$40.000</td>
                </tr>
                <tr id="2">
                  <td>Lavadora</td>
                  <td>$40.000</td><!-- necesito quitar los signos-->
                </tr>
              </tbody>
      </table>

JS:     // go through the value td and add all the values

var Total = 0;
$('#table tbody > tr').each(function () {
    Total += parseInt($(this).find('td:eq(1)').text());                       
    });
  alert(Total);//me retorna un NaN por que contiene signos
    
asked by Javier Antonio Aguayo Aguilar 13.07.2017 в 18:19
source

1 answer

1

You just need to delete the currency sign and the floating point sign.

Example

var Total = 0;
$('#table tbody > tr').each(function() {
  let qty = 
  	$(this)
      .find('td:eq(1)')
      .text()
  	  .replace(/\$/, '')
      .replace(/\./, '');
  Total += Number(qty);
});
alert(Total); //me retorna un NaN por que contiene signos
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table">
  <thead>
    <tr>
      <th>Producto</th>
      <th>valor</th>
    </tr>
  </thead>

  <tbody>
    <tr id="1">
      <td>Licuadora</td>
      <td>$40.000</td>
    </tr>
    <tr id="2">
      <td>Lavadora</td>
      <td>$40.000</td>
      <!-- necesito quitar los signos-->
    </tr>
  </tbody>
</table>

However, I imagine that later you will have numbers with a specific format for your country's currency; for this purpose it is better to use the library numeral.js .

Example

(function() {
  // registra un formato de número, esto es
  // especial cuando guardas números como moneda
  // de tu país, la cual tiene un formato específico.
  numeral.register('locale', 'mx', {
    delimiters: {
      thousands: '.',
      decimal: ','
    },
    abbreviations: {
      thousand: 'k',
      million: 'm',
      billion: 'b',
      trillion: 't'
    },
    currency: {
      symbol: '$'
    }
  });
  numeral.locale('mx'); // usa el formato que hemos creado
  
  var Total = 0;
  $('#table tbody > tr').each(function() {
    let qty = 
      $(this)
        .find('td:eq(1)')
        .text();
    // hacemos uso de la librería para que parsee
    // el número y solo obtenemos el valor correspondiente
    // para JavaScript usando el método value.
    Total += numeral(qty).value();
  });
  alert(Total); //me retorna un NaN por que contiene signos
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>

<table id="table">
  <thead>
    <tr>
      <th>Producto</th>
      <th>valor</th>
    </tr>
  </thead>

  <tbody>
    <tr id="1">
      <td>Licuadora</td>
      <td>$40.000</td>
    </tr>
    <tr id="2">
      <td>Lavadora</td>
      <td>$40.000</td>
      <!-- necesito quitar los signos-->
    </tr>
  </tbody>
</table>
    
answered by 13.07.2017 / 18:44
source