Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

javascript - Turn properties of object into a comma-separated list?

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?

question from:https://stackoverflow.com/questions/26732123/turn-properties-of-object-into-a-comma-separated-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use this one-liner in modern browsers

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...