Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn properties of object into a comma-separated list?

Tags:

javascript

csv

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?

like image 271
areim Avatar asked Nov 04 '14 09:11

areim


People also ask

How do you convert lists into comma separated values?

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.

What is a comma-separated list example?

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.

How do you turn an object into a string in JavaScript?

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.


1 Answers

You can use this one-liner in modern browsers

Object.keys(person).map(function(k){return person[k]}).join(","); 
like image 193
Joschi Avatar answered Sep 19 '22 17:09

Joschi