简体   繁体   English

如何将PascalCase属性中的JavaScript对象克隆到camelCase属性(在JavaScript中)?

[英]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.

相关问题 Javascript:将返回的 JSON Object 属性转换为 PascalCase - Javascript: Convert returned JSON Object Properties to PascalCase 如何从数组中的 JavaScript 对象中删除属性? - How do I remove properties from JavaScript object in array? 如何在JavaScript中创建具有对象属性的对象类? - How do I create an object class with object properties in JavaScript? 如何将Javascript对象的所有属性放入另一个对象? - How do I place all properties of Javascript object into another object? 如何访问放置了javascript对象的对象的属性? - How do I access the properties of objects that are placed a javascript object? 如何在javascript对象中使用构造函数以仅使用`this`中的属性? - How do I use the constructor in a javascript object to only use the properties in `this`? 如何在JavaScript中迭代Object原型的属性? - How do I iterate over the properties of an Object's prototype in JavaScript? 如何在JavaScript中将对象属性添加到数组中? - How do I add object properties into an array in JavaScript? 我如何从 map 到 javascript object 到 class 并删除它不需要的属性 - How do i map a javascript object to a class and remove unwanted properties in it 如何遍历JavaScript对象的深层嵌套属性? - How do I loop through deeply nested properties of a JavaScript object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM