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

203
Vistas
Get concatened string from variable in javascript object with map function

I have this object below:

var products = [
        {
           "id":"xxx",
           "name":"AAA"
        },
        {
           "id":"xxx",
           "name":"BBB"
        }
     ];

I´m trying to mapping the object and generate a concatenated string from all the values ´name´ using the map function and then join to concatenate the comma:

var result = products.map(p => Object.values(p.name).join(','));

But I'm getting this output with console.log:

"A,A,A, ,B,B,B"

The is one more comma there and plus it takes each character instead of the full key name value. Am I not accessing the array object in the right way?

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

0

Currently you're turning a string into an array and joining each character with a comma between them. You need to move the join outside of the map iterator function. There is also no need for the Object.value() call.

const result = products
  .map((product) => product.name)
  .join(', ')

var products = [
  {
     "id":"xxx",
     "name":"AAA"
  },
  {
     "id":"xxx",
     "name":"BBB"
  }
];

const result = products.map((product) => product.name).join(', ')

document.getElementById('root').innerHTML = result
<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use Array.map and Array.join

Logic

  • Create a list of names from products using Array.map.
  • From that list create a string with Array.join

var products = [{"id":"xxx","name":"AAA"},{"id":"xxx","name":"BBB"}];
console.log(products.map(item => item.name).join(','))

about 4 years ago · Juan Pablo Isaza Denunciar

0

I really don't understand your questions but i guess you're trying to concatenate the name values .If Yes,use the below code.

var result = products.map(p => Object.values(p.name).join('')).join('');
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