[英]How do I clone a JavaScript object from PascalCase properties to camelCase properties (in JavaScript)?
When I serialize an ASP.NET MVC form, I get this: 当我序列化ASP.NET MVC表单时,我得到了这个:
{
DestinationId: "e96dd00a-b042-41f7-bd59-f369904737b6",
...
}
But I want this, so that it's consistent with JS coding conventions: 但是我想要这个,所以它与JS编码约定一致:
{
destinationId: "e96dd00a-b042-41f7-bd59-f369904737b6",
...
}
How do I take an object and lower-case the first character of each property? 如何将对象和小写字体作为每个属性的第一个字符?
Simple way is make iteration over your object: 简单的方法是对您的对象进行迭代:
var newObj = {};
for (var p in o) {
newObj[p.substring(0,1).toLowerCase()+p.substring(1)] = o[p];
}
Also have to check for hasOwnProperty, and remove the previous property 还必须检查hasOwnProperty,并删除以前的属性
var object = { DestinationId: "e96dd00a-b042-41f7-bd59-f369904737b6" }
for ( var prop in object ) {
if ( object.hasOwnProperty( prop ) ) {
object[ prop.substring(0,1).toLowerCase() + prop.substring(1) ] = object[ prop ];
delete object[ prop ];
}
}
将这些东西视为字符串并使用Reg exps power是不是更好?
JSON.parse( JSON.stringify(z).replace( /(\\"[^"])([^"]*\\"\\:)/g, function(all, head, tail) { return head.toLowerCase() + tail; } ) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.