46

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?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
areim
  • 3,371
  • 2
  • 23
  • 29
  • As a reference, this is in essence just [getting the object values as an array](https://stackoverflow.com/q/7306669/1218980) and joining them with `join`. – Emile Bergeron Nov 28 '17 at 16:58

12 Answers12

87

You can use this one-liner in modern browsers

Object.keys(person).map(function(k){return person[k]}).join(",");
Joschi
  • 2,874
  • 1
  • 18
  • 23
26

This code will be useful when you want to extract some property values from an array of objects as comma separated string.

    var arrayObjects = [
  {
    property1: "A",
    property2: "1"
  },
  {
    property1: "B",
    property2: "2"
  },
  {
    property1: "C",
    property2: "3"
  }
];

Array.prototype.map.call(arrayObjects, function(item) { return item.property1; }).join(",");

Output - "A,B,C"

Sachin Gaur
  • 679
  • 7
  • 6
15

Here is a simpler one liner. It uses the Object.values() method instead of Object.keys

Object.values(obj).join(",");
Jack Fairfield
  • 1,876
  • 22
  • 25
7

try this:

var key,
  person = {
    name: "John",
    surname: "Smith",
    phone: "253 689 4555"
  },
  array = [];

for ( key in person ) {
  if ( person.hasOwnProperty( key ) ) {
    array.push( person[ key ] );
  }
}

console.log( array.join( ',' ) );

or in function style:

var
  getValues = function ( obj ) {
    var key,
      array = [];

    for ( key in obj ) {
      if ( obj .hasOwnProperty( key ) ) {
        array.push( obj [ key ] );
      }
    }

    return obj.join( ',' );
  };

var person = {
      name: "John",
      surname: "Smith",
      phone: "253 689 4555"
    };

console.log( getValues( person ) );
TheGr8_Nik
  • 3,080
  • 4
  • 18
  • 33
7

Write a function like this:

function toCSV(obj, separator) {
    var arr = [];

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            arr.push(obj[key]);
        }
    }

    return arr.join(separator || ",");
}

And then you can call it like:

toCSV(person, "|"); // returns "John|Smith|253 689 4555"

or

toCSV(person); // returns "John,Smith,253 689 4555"
Vikas Gupta
  • 1,183
  • 1
  • 10
  • 25
Kenny Ki
  • 3,400
  • 1
  • 25
  • 30
7
var arr = [
    {
        key_1: "A",
        key_2: "1"
    },
    {
        key_1: "B",
        key_2: "2"
    },
    {
        key_1: "C",
        key_2: "3"
    }
];

var CSVOf_arr = arr.map((item) => { return item.key_1 }).join(',')

console.log('Comma seprated values of arr', CSVOf_arr)

OUTPUT: A,B,C

Hardik Desai
  • 1,089
  • 12
  • 20
2

You might want to try this library - Underscore.js. It gives you really helpful methods and works across browsers.

Here you can do it like this -

_.values(person).join(",")

or

_.values(person).join("|")
vatsal
  • 3,803
  • 2
  • 19
  • 19
2

The Object.values() method returns an array of a given object's own enumerable property values. It can be used to convert object properties to an array which can then be joined using .join(separator) to create the required string with separator defined in join function. But this will cause empty values to be joined as well. So for an object as

var person = {
  name: "John",
  surname: "Smith",
  phone: ""
}

output for

Object.values(person).join(',')

will be

John,Smith,

with an additional "," at the end. To avoid this .filter() can be used to remove empty elements from array and then use join on filtered array.

Object.values(person).filter(Boolean).join(',')

will output

John,Smith

Arshad
  • 409
  • 4
  • 7
0
Object.values(person).join('/')
J.Du
  • 271
  • 3
  • 4
0

Another way is to use lodash function _.toString()

console.log( _.toString( Object.values( person ) ) );
==> John,Smith,253 689 4555

Check the link below https://lodash.com/docs/4.17.5#toString

Velen Aranha
  • 1
  • 1
  • 3
0
var person = {
    name: "John",
    surname: "Smith",
    phone: "253 689 4555"
 }



let personData = ${person.name},${person.surname},${person.phone}

With this `` mark and $ dollar sign you will get the result.

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
0

Just use object.values() and use toString() method

Object.values(person).toString()
Sai Jeevan Balla
  • 135
  • 3
  • 10