Turn properties of object into a comma-separated list?

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"


You can use this one-liner in modern browsers

Object.keys(person).map(function(k){return person[k]}).join(",");

Tags:

Javascript

Csv