Javascript 使用 underscore.js 过滤 js 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11924976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
filter through js objects using underscore.js
提问by user1551482
I am trying to filter through this javascript object using underscore.js, but I don't know why it's not working, its meant to find any question value that has "how" in it.
我正在尝试使用 underscore.js 过滤这个 javascript 对象,但我不知道为什么它不起作用,它的目的是找到任何包含“如何”的问题值。
var questions = [
{question: "what is your name"},
{question: "How old are you"},
{question: "whats is your mothers name"},
{question: "where do work/or study"},
];
var match = _.filter(questions),function(words){ return words === "how"});
alert(match); // its mean to print out -> how old are you?
the full code is here(underscore.js already included): http://jsfiddle.net/7cFbk/
完整代码在这里(underscore.js 已经包含):http: //jsfiddle.net/7cFbk/
回答by pimvdb
- You closed the function call with
.filter(questions)
. The last)
shouldn't be there. - Filtering works by iterating over the array and calling the function with each element. Here, each element is an object
{question: "..."}
, not a string. - You check for equality, whereas you want to check whether the question string containsa certain string. You even want it case-insensitive.
- You cannot alert objects. Use the console and
console.log
instead.
- 你用 关闭了函数调用
.filter(questions)
。最后一个)
不应该在那里。 - 过滤的工作原理是遍历数组并使用每个元素调用函数。在这里,每个元素都是一个对象
{question: "..."}
,而不是一个字符串。 - 您检查相等性,而您想检查问题字符串是否包含某个字符串。您甚至希望它不区分大小写。
- 您不能提醒对象。改用控制台
console.log
。
So: http://jsfiddle.net/7cFbk/45/
所以:http: //jsfiddle.net/7cFbk/45/
var questions = [
{question: "what is your name"},
{question: "How old are you"},
{question: "whats is your mothers name"},
{question: "where do work/or study"},
];
var evens = _.filter(questions, function(obj) {
// `~` with `indexOf` means "contains"
// `toLowerCase` to discard case of question string
return ~obj.question.toLowerCase().indexOf("how");
});
console.log(evens);
回答by David Weldon
Here is a working version:
这是一个工作版本:
var questions = [
{question: "what is your name"},
{question: "How old are you"},
{question: "whats is your mothers name"},
{question: "where do work/or study"},
];
var hasHow = _.filter(questions, function(q){return q.question.match(/how/i)});
console.log(hasHow);
issues fixed:
修复的问题:
- Parens were not correctly placed.
- Use
console.log
instead of alert. - You should probably use a regexp to find 'how' when iterating over each question.
_filter
iterates over an array. Your array contains objects, and each object contains a question. The function you pass to_filter
needs to examine each object in the same way.
- 父母未正确放置。
- 使用
console.log
而不是警报。 - 在迭代每个问题时,您可能应该使用正则表达式来查找“如何”。
_filter
迭代一个数组。您的数组包含对象,每个对象包含一个问题。您传递给的函数_filter
需要以相同的方式检查每个对象。
回答by user1975073
data = {
'data1' :{
'name':'chicken noodle'
},
'data2' :{
'name':'you must google everything'
},
'data3' :{
'name':'Love eating good food'
}
}
_.filter(data,function(i){
if(i.name.toLowerCase().indexOf('chick') == 0){
console.log(i);
}else{
console.log('error');
}
})