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

215
Vistas
add text to end of array items depending on the item number

I've got a dropdown list of numbers which I intend to use to track my miles. I want to add the text 'miles' after the number but if the number is 1, I want to add 'mile'.

unit = miles | arrayOfArrays = [0, 1, 2, 3, 4, 5, 6, 7, 8]

Here is my script

function showQtyNumbers(el, arrayOfArrays, unit) {
    alert(arrayOfArrays);
    el.innerHTML = '';
    arrayOfArrays.forEach(function(r) {
        var option = document.createElement('option');
        option.setAttribute('value', r);
        option.text = r + ' ' + unit;
        el.appendChild(option);
    });
  }

That gives me the results: 0 miles, 1 miles, 2 miles etc.

I've tried the following but it does not work.

function showQtyNumbers(el, arrayOfArrays, unit) {
    number1 = arrayOfArrays.find(e => e === 1)
    el.innerHTML = '';
    arrayOfArrays.forEach(function(r) {
        var option = document.createElement('option');
        if (r.find(e => e === 1)) {
          r + unit.substring(0, unit.length - 1)
        } else {
           option.text = r + unit; 
        };
        option.setAttribute('value', r);
       
        
        el.appendChild(option);
    });
  }

I Know arrayOfArrays.find(e => e === 1) finds 1 but I don't know how to do it in forEach.

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

0

Why don't you pass the value for unit to the function as singular and append an 's' if the value is not 1? Like this:

 if(r!=1){ option.text = r + unit +"s"; }else{ option.text = r + unit; }
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