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?
如果可能的话,您能否提供一个例子,我也可以定义分离器?
7 个解决方案
#1
26
You can use this one-liner in modern browsers
您可以在现代浏览器中使用这一行代码
Object.keys(person).map(function(k){return person[k]}).join(",");
#2
5
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"
#3
4
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 ) );
#4
2
You might want to try this library - Underscore.js. It gives you really helpful methods and works across browsers.
您可能想尝试这个库- Underscore.js。它提供了非常有用的方法,可以跨浏览器工作。
Here you can do it like this -
你可以这样做
_.values(person).join(",")
or
或
_.values(person).join("|")
#5
0
Object.values(person).join('/')
#6
0
Here is a simpler one liner. It uses the Object.values() method instead of Object.keys
这是一个比较简单的。它使用Object.values()方法而不是Object.keys
Object.values(obj).join(",");
#7
0
Another way is to use lodash function _.toString()
另一种方法是使用lodash函数_.toString()
console.log( _.toString( Object.values( person ) ) );
==> John,Smith,253 689 4555
Check the link below https://lodash.com/docs/4.17.5#toString
检查下面的链接https://lodash.com/docs/4.17.5#toString
#1
26
You can use this one-liner in modern browsers
您可以在现代浏览器中使用这一行代码
Object.keys(person).map(function(k){return person[k]}).join(",");
#2
5
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"
#3
4
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 ) );
#4
2
You might want to try this library - Underscore.js. It gives you really helpful methods and works across browsers.
您可能想尝试这个库- Underscore.js。它提供了非常有用的方法,可以跨浏览器工作。
Here you can do it like this -
你可以这样做
_.values(person).join(",")
or
或
_.values(person).join("|")
#5
0
Object.values(person).join('/')
#6
0
Here is a simpler one liner. It uses the Object.values() method instead of Object.keys
这是一个比较简单的。它使用Object.values()方法而不是Object.keys
Object.values(obj).join(",");
#7
0
Another way is to use lodash function _.toString()
另一种方法是使用lodash函数_.toString()
console.log( _.toString( Object.values( person ) ) );
==> John,Smith,253 689 4555
Check the link below https://lodash.com/docs/4.17.5#toString
检查下面的链接https://lodash.com/docs/4.17.5#toString