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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:41:37  来源:igfitidea点击:

filter through js objects using underscore.js

javascriptfilter

提问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

  1. You closed the function call with .filter(questions). The last )shouldn't be there.
  2. Filtering works by iterating over the array and calling the function with each element. Here, each element is an object {question: "..."}, not a string.
  3. You check for equality, whereas you want to check whether the question string containsa certain string. You even want it case-insensitive.
  4. You cannot alert objects. Use the console and console.loginstead.
  1. 你用 关闭了函数调用.filter(questions)。最后一个)不应该在那里。
  2. 过滤的工作原理是遍历数组并使用每个元素调用函数。在这里,每个元素都是一个对象{question: "..."},而不是一个字符串。
  3. 您检查相等性,而您想检查问题字符串是否包含某个字符串。您甚至希望它不区分大小写。
  4. 您不能提醒对象。改用控制台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.loginstead of alert.
  • You should probably use a regexp to find 'how' when iterating over each question.
  • _filteriterates over an array. Your array contains objects, and each object contains a question. The function you pass to _filterneeds 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');

    }

})