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

149
Visualizações
how to convert array of object to comma separated array of object

I have array of object and I want to convert this to comma separated object.

let arr = [{name:"k"},{name:"p"}];
  console.log(...arr.join(','));

I'm getting this [ o b j e c t O b j e c t ] , [ o b j e c t O b j e c t ] I want to in this format {name:"k"},{name:"p"}.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The default conversion of plain objects to strings results in "[object Object]". (More about why you got the specific output you got below.) If you want something different, you have to do that with your own code.

For instance, you could use map to convert the objects to strings, then use join on the result:

const result = arr.map(({name}) => `{name: ${JSON.stringify(name)}`).join(",");

Live Example:

let arr = [{name:"k"},{name:"p"}];
const result = arr.map(({name}) => `{name: ${JSON.stringify(name)}}`).join(",");
console.log(result);

That just does the one specific property of course. If you want to include others, you'll need a loop/mapping operation in the map callback (perhaps starting with Object.entries on the object).

const result = arr.map(obj => {
    const props = Object.entries(obj).map(([name, value]) => `${name}: ${JSON.stringify(value)}`);
    return `{${props}}`;
}).join(",");

Live Example:

let arr = [{name:"k"},{name:"p"}];
const result = arr.map(obj => {
    const props = Object.entries(obj).map(([name, value]) => `${name}: ${JSON.stringify(value)}`);
    return `{${props}}`;
}).join(",");
console.log(result);

If you don't mind quotes around the property names, then as shobe said you can do that by applying JSON.stringify to the entire object.

const result = arr.map(obj => JSON.stringify(obj)).join(",");

But your question didn't show quotes around property names.

Live Example:

let arr = [{name:"k"},{name:"p"}];
const result = arr.map(obj => JSON.stringify(obj)).join(",");
console.log(result);


Why you got the specific result you got:

I was initially surprised by the output you got, but it does make sense after a moment's thought: Your code does console.log(...arr.join(",")), which does this:

  • Calls join on arr, which combines its entries together into a string using their default convert-to-string behavior, with a single comma in-between.
  • Spreads out that string into discrete arguments to console.log as though you'd written console.log("[", "o", "j" etc.
about 4 years ago · Juan Pablo Isaza Relatório

0

Why not! just turn it to JSON format, then remove open or closed square-brackets:

let arr = [{name:"k"},{name:"p"}];
console.log(JSON.stringify(arr).replace(/\[|\]/g,''))

Result as expected: {"name":"k"},{"name":"p"}

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