admin管理员组文章数量:1292170
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 ?
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 ?
Share Improve this question edited Mar 28, 2013 at 0:58 pb2q 59.6k19 gold badges150 silver badges152 bronze badges asked Mar 19, 2013 at 21:24 duivvvduivvv 111 gold badge1 silver badge5 bronze badges 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. – bfavaretto Commented Mar 19, 2013 at 21:27 - I know what toLowerCase() is :) .. It's JSON – duivvv 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())
. – bfavaretto 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 plete string. – duivvv 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. – Scott Sauyet Commented Dec 2, 2014 at 15:46
4 Answers
Reset to default 6You can do this easily with Lo Dash's _.mapValues
function:
_.mapValues(objs, function(s){
return _.isString(s) ? s.toLowerCase() : s;
});
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/uY36J/
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();
});
});
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. ¯\_(ツ)_/¯
本文标签: javascriptLowercase of object value in array with underscore jsStack Overflow
版权声明:本文标题:javascript - Lowercase of object value in array with underscore js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741547898a2384704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论