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

250
Vistas
Why might concat not be a function, even with two strings?

I am generating a dropdown menu and I would like each option in the dropdown menu to be labelled with two strings (product name and company name) and have a serial number as a value attribute. These are each obtained from JSON (contained in a variable called list).

Here is a sample record from the JSON:

[
    {
        'code': '01234567', 
        'company': 'SAMPLE COMPANY', 
        'name': 'SAMPLE PRODUCT'
    }
]

This should be presented in the HTML like so:

<option value="01234567">SAMPLE PRODUCT [SAMPLE COMPANY]</option>

My Javascript is as follows:

for (let i = 0; i < l; i++) {
    var opt = document.createElement("OPTION");
    opt.setAttribute("value", list[i]["code"]);
    var product = list[i]["name"];
    var company = list[i]["company"];
    opt.innerHTML = product.concat(" [", company, "]");
}

However, this fails with "Uncaught TypeError: product.concat is not a function". I've checked the data type for product and company and they are both strings in all instances.

What else might be causing this type error?

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

0

// product is string type, just join them with "+"
// concat is a method of array type
opt.innerHTML = product + " [" + company + "]";
// tempalte string in ES6
opt.innerHTML = `${product} [${company}]`;
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use template literal as

opt.innerHTML = `${product} [${company}]`;

const list = [{
  'code': '01234567',
  'company': 'SAMPLE COMPANY',
  'name': 'SAMPLE PRODUCT'
}]

for (let i = 0; i < list.length; i++) {
  var opt = document.createElement("OPTION");
  opt.setAttribute("value", list[i]["code"]);
  var product = list[i]["name"];
  var company = list[i]["company"];
  opt.innerHTML = `${product} [${company}]`;
  document.body.appendChild(opt)
}

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