Turn properties of object into a comma-separated list?

前端 未结 10 1713
-上瘾入骨i
-上瘾入骨i 2021-02-01 02:42

I have an object like this:

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

I want:



        
10条回答
  •  滥情空心
    2021-02-01 03:31

    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"

提交回复
热议问题