1

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 ?

5
  • 1
    Is it really JSON (i.e., a string), or is it an object? If it's a string, you can just .toLowerCase() it. Commented Mar 19, 2013 at 21:27
  • I know what toLowerCase() is :) .. It's JSON Commented Mar 19, 2013 at 22:02
  • 2
    That's my point. JSON is always a string representation of an object. If it's not a string, it's not JSON. So, if it's an object, maybe you could use var transformed = JSON.parse(JSON.stringify(obj).toLowerCase()). Commented Mar 19, 2013 at 22:04
  • ok but I only want a certain property for all objects in the array to lowercase. Not the complete string. Commented Mar 19, 2013 at 22:08
  • In your example, you have only one field with alphabetic characters in it. Are there others in the real data? If not, bfavaretto's solution is a very simple one. Commented Dec 2, 2014 at 15:46

4 Answers 4

6

You can do this easily with Lo Dash's _.mapValues function:

_.mapValues(objs, function(s){
  return _.isString(s) ? s.toLowerCase() : s;
});
Sign up to request clarification or add additional context in comments.

Comments

1

If you're already dealing with an object (or parsed JSON), you can loop and create a new one:

var objs = [{"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"}];

var out = [];
for(var i=0; i<objs.length; i++) {
    var outObj = {}
    for(var prop in objs[i]) {
        var val = objs[i][prop];
        if(prop === 'title') {
            val = val.toLowerCase();  
        }
        outObj[prop] = val;
    }
    out.push(outObj);
}
console.log(out);

http://jsfiddle.net/uY36J/

2 Comments

It should be clear from my answer that I'm not familiar with underscore. Maybe the library provides methods to make that shorter. An obvious candidate (that doesn't even require underscore) would be a map call instead of the outer loop.
I am looking for a shorter (underscore?) solution.
1

If you have array of objects:

for(var i = 0; i < array.length; i++) {
   for (var prop in array[i])
       // condition here
       array[i][prop] = array[i][prop].toLowerCase();
}

console.log(array)

Same with underscore (I don't think it's much shorter - you still need two loops here. More readable - maybe, but not shorter)

_.each(array, function(obj) {
    _.each(obj, function(value, key) { 
        // condition here        
        obj[key] = value.toLowerCase();
    });
});

1 Comment

As stated, I am looking for a shorter (with underscore?) way to achieve this
0

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.

var objs = [{"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"}];

 _.map(objs,function(element) {
     return  _.object(_.keys(element), _.values(element).map(function(value) { 
         return _.isString(value)? value.toLowerCase():value; 
     }));
 });

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

 _.map(objs,function(element) {
     return  _.object(
         _.keys(element).map(function(key) { 
             return _.isString(key)? key.toLowerCase():key; 
         }),
         _.values(element).map(function(value) { 
             return _.isString(value)? value.toLowerCase():value; 
         })
     );
 });

Caveat: this will not operate on nested objects. ¯\_(ツ)_/¯

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.