1

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"}.

1
  • If you were happy for the output to be just similar to what you asked for, rather than precisely what you asked for, you should have said that in the question. Commented Dec 9, 2021 at 8:13

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

3 Comments

thanks T.J. Crowder.
good approaches, but why do you loop over each obj in array turn it to JSON then join them back, instead of just returning the whole array into JSON then removing brackets by replacing them with empty strings?
@TAHERElMehdi - You mean the third snippet that produces JSON? It just seemed cleaner. There would be a loop inside JSON.stringify anyway. :-)
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"}

1 Comment

"Result as expected:" It isn't, though. The OP asked for {name:"k"},{name:"p"}, this gives them {"name":"k"},{"name":"p"}. Apparently being just similar is good enough for them, though. :-) Would have been good if they'd said that in the question. :-|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.