I have an object like this:
var person = {
name: \"John\",
surname: \"Smith\",
phone: \"253 689 4555\"
}
I want:
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