[英]How to add quotation marks to key-value object?
How can I change this : 我怎么能改变这个:
{"position":4}
to this : 对此:
{"position":"4"}
Is there any simpe function (in javascript or node.js package), that puts quots around value ? 是否有任何simpe函数(在javascript或node.js包中),将值放在值周围?
Maybe this (add quotation marks to the string): 也许这个(在字符串中加上引号):
var obj = {position: 4};
for (var p in obj)
obj[p] = '"' + obj[p] + '"';
Or this (convert number to string): 或者这个(将数字转换为字符串):
var obj = {position: 4};
for (var p in obj)
obj[p] = obj[p].toString();
i go whith 4+""
as that castes it to a string. 我把它变成4+""
,因为它将它变成一个字符串。
but you probly wanner rethink Why you need to cast your number to a string. 但你很想重新思考为什么你需要把你的号码转换成一个字符串。
I don't think there is any function as such but you may do it with something like: 我不认为有这样的功能,但你可以这样做:
EDIT: If you don't know the property value, do like this: 编辑:如果您不知道属性值,请执行以下操作:
var test=new Object();
test.position="4";
for (var prop in test) {
test[prop]="\""+test[prop]+"\"";
alert(prop + " = " + test[prop]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.