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

156
Vistas
Add comma after object value in array

I am trying to get a result like the following in order to call a SQL query. It's an array of objects (each object with a key, that's going to be VALUE and a value. The value must be into single quotes with a comma at the end):

[
  { VALUE: 'A', },
  { VALUE: 'B', },
  { VALUE: 'C', },
  { VALUE: 'D', },
  { VALUE: 'E', },
  { VALUE: 'F' }
]

I've tried using a for function in which I add an object for each element in a list. This is the result I get:

Code:

    let arr = [];
    const len = reqBody.values.length;

    for (let i = 0; i < len; i++)
    {
        arr.push( { VALUE: reqBody.values[i] } )
    }

Result:

[
  { VALUE: 'A' },
  { VALUE: 'B' },
  { VALUE: 'C' },
  { VALUE: 'D' },
  { VALUE: 'E' },
  { VALUE: 'F' }
]

I've tried using a map arrow function with a join at the end of it in order to try to add the comma after each value (so for the second line of the result code would be something like: { VALUE: 'A', }) but I cant make it work since that returns an array of strings, not objects like the ones I need.

This is the code described above:

Code:

let arr = [];
const len = reqBody.values.length;

for (let i = 0; i < len; i++)
{
    arr.push(Object.values({ VALUE: reqBody.values[i] }).map(v => `{ VALUE: '${v}', }`).join(','))
}

Result:

[
  "{ VALUE: 'A', }",
  "{ VALUE: 'B', }",
  "{ VALUE: 'C', }",
  "{ VALUE: 'D', }",
  "{ VALUE: 'E', }",
  "{ VALUE: 'F', }",
]

Is there any way to cast that string in the map function to be an object?

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

0

You need to construct the entire thing as a concatenated string, not as an array.

let reqBody = {
  values: ['A', 'B', 'C']
};

let items = reqBody.values.map(v => `{ VALUE: '${v}', }`);
let result = '[\n  ' + items.join(',\n  ') + '\n]';
console.log(result);

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