I have an object like this:
var person = { name: "John", surname: "Smith", phone: "253 689 4555" }
I want:
John,Smith,253 689 4555
Is there some easy way?
If possible, could you please provide an example where I can also define the separator?
Approach: This can be achieved with the help of join() method of String as follows. Get the List of String. Form a comma separated String from the List of String using join() method by passing comma ', ' and the list as parameters. Print the String.
For example, C{:,1} is a comma-separated list if C has more than one row. A comma-separated list is produced for a structure array when you access one field from multiple structure elements at a time.
Stringify a JavaScript ObjectUse the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.
You can use this one-liner in modern browsers
Object.keys(person).map(function(k){return person[k]}).join(",");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With