Let's say I have this JSON data in a data variable
[{"id":"1","module_id":"1","title":"Test",
"start_date":"2012-11-12" "end_date":"2012-11-18"},
{"id":"8","module_id":"1","title":"This is OK",
"start_date":"2013-01-14","end_date":"2013-01-31"}]
How do I use underscore.js to get following result?
[{"id":"1","module_id":"1","title":"test",
"start_date":"2012-11-12","end_date":"2012-11-18"},
{"id":"8","module_id":"1","title":"this is ok",
"start_date":"2013-01-14","end_date":"2013-01-31"}]
Can I do this with invoke ?
You can do this easily with Lo Dash's
_.mapValues
function:If you're already dealing with an object (or parsed JSON), you can loop and create a new one:
http://jsfiddle.net/uY36J/
If you have
array
of objects:Same with underscore (I don't think it's much shorter - you still need two loops here. More readable - maybe, but not shorter)
You can separate the object in two arrays, one with the keys and the other with the values, then use _.map to lowercase the strings.
you see, I'm checking if it's a string we're dealing with, to avoid calling toLowerCase on other types.
Lowercasing both keys and values is trivial as well
Caveat: this will not operate on nested objects. ¯|(ツ)/¯