Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

295
Vistas
How to add commas to thousands numbers coming from API using JavaScript

Link to live code.

I'm currently pulling in API data for products that have price points in the thousands. Below, I've tried adding the code toLocaleString() to the price, which in full below is <p id="price">$${product.price?.text.toLocaleString() ?? ""}</p>. However it doesn't seem to add a comma to make the price read in the thousands (example - 1,000).

JS Code

// Fetch Data
async function getData() {
  const res = await fetch(url);
  const data = await res.json();
  let output = "";
  // Loop through first 'groups' array
  data.groups.map(function (group) {
    // Loop through each 'equipments' array
    group.equipments.map((product) => {
      // Define below variable to match cat products only
      const catProducts =
        product["dealer-name"] === "DEALER NAME";
      // If the dealer name is everything but cat products (aka only bfe products)..
      if (!catProducts) {
        // Loop through each 'photos' array
        product.photos.map(() => {
          // Then output the data
          // If year is undefined, replace with empty string
          output += `
            <div class="card">
            <img class="img-fluid" src=${product.photos[0].text} alt=${
            product.model
          } />
            <div class="card--body">
              <h3>${product.year ?? ""} ${product.manufacturer} ${
            product.model ?? ""
          }</h3>
              <p>${product.city ?? "City Not Available"}, ${product.state}</p>
              <p>${product.hours} hours</p>
              <p id="price">$${product.price?.text.toLocaleString() ?? ""}</p> <--- Not working
              <a href='https://used.battlefieldequipment.ca/en/${
                product["group-code"]
              }/${
            product["serial-number"]
          }' class="btn btn-primary">View Details</a>
              </div>
            </div>         
        `;
        });
      }
    });
  });
  // Add to slider
  $(".used-slider").slick("slickAdd", output);
}

getData();

Any idea how I can get the API prices to show up with a comma, making them show properly as thousands numbers?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The number has to be a number rather than a string for that to work. parseFloat(numberAsString).toLocaleString()

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda