Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clone a JavaScript object from PascalCase properties to camelCase properties (in JavaScript)?

Tags:

javascript

When I serialize an ASP.NET MVC form, I get this:

{
  DestinationId: "e96dd00a-b042-41f7-bd59-f369904737b6",
  ...
}

But I want this, so that it's consistent with JS coding conventions:

{
  destinationId: "e96dd00a-b042-41f7-bd59-f369904737b6",
  ...
}

How do I take an object and lower-case the first character of each property?

like image 667
Josh Kodroff Avatar asked Oct 30 '12 13:10

Josh Kodroff


1 Answers

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];
}
like image 69
Damask Avatar answered Sep 26 '22 02:09

Damask