简体   繁体   中英

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 [ obje c t O bje c t ], [ obje c t O bje c t ] I want to in this format {name:"k"},{name:"p"} .

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.

In order to convert object to it's string representation, you need to use JSON.stringify function. So it would look like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM