Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda