7

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.

  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/

3 Answers 3

15
  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 contains a certain string. You even want it case-insensitive.
  4. You cannot alert objects. Use the console and console.log instead.

So: 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);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks works very good, how can pass these values into a html element, adding them to a div i.e. foreach question found $('divname').html('<p>' + even.obj + '<p>'); thanks :)
I think you need to elaborate a bit. But you'll need _.each I think.
To others new to Javascript, and didn't quite catch the comment in the example, the '~' at the beggining of the return line is important and the full line is equivalent to "obj.question.toLowerCase().indexOf("how") >= 0" in this specific case.
3

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.

Comments

0
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');

    }

})

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.