使用下划线过滤JSON [英] filtering JSON using underscore
本文介绍了使用下划线过滤JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到一个JSON名字集合是这样的:
I got a JSON name collection like this:
[{
"name":"Jim",
"prize":"Nokia Mobile"
},
{
"name":"Marry",
"prize":"iPhone"
},
{
"name":"Alice",
"prize":"iPad"
}
]
现在我想用一个字符串来过滤此JSON:VAR数据=M;
now I'd like to filter this JSON by a string: var data= "m";
这样,这是我做了什么:
so, this is what I've done:
var data= "m";
collection = _.filter(collection,function(c){
return _.any(c,function(k){
return k.name.toLowerCase().indexOf(data.toLowerCase()) != -1;
});
});
如此,在这种情况下,集合返回其具有m在其名称的元素
so, in this case, collection return elements which has m in their name.
不过,我已经得到了不能调用未定义的方法'与toLowerCase'。
However, I've got "Cannot call method 'toLowerCase' of undefined".
我试图把警报警报(c.name);最后返回之前,它的返回未定义。它不应该是,是不是?
I tried to put an alert "alert(c.name);" before the last return, and it's return "undefined". It shouldn't be, isn't it?
什么需要我改变来解决这个问题?
what need I change to fix this?
推荐答案
请参阅此 http://underscorejs.org/#过滤器
试试这个:
var data= "m";
collection = _.filter(collection,function(myObject){
return myObject.name.toLowerCase().indexOf(data.toLowerCase()) != -1;
});
希望它帮助。
这篇关于使用下划线过滤JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文