Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert each value of an array object into single string?

Array Given:

const Collection = [{ id: 1 }, { id: 3 }, { id: 2 }, { id: 4 }]

Required String should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

string = "1,3,2,4"

I have tried to get from forEach() loop to join() each value and save it in a string variable.

expecting result should be a comma separated string.

>Solution :

You can use forEach, but you would have to pair it with something else.

const Collection = [{ id: 1 }, { id: 3 }, { id: 2 }, { id: 4 }];
var res = [];

Collection.forEach(function(item) {
    res.push(item.id);
});

res = res.join(",");
console.log(res);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading