Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!