Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter through js objects using underscore.js

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/

like image 817
user1551482 Avatar asked Aug 12 '12 19:08

user1551482


People also ask

Can I filter through an object JavaScript?

JavaScript's Objects are not iterable like arrays or strings, so we can't make use of the filter() method directly on an Object . filter() allows us to iterate through an array and returns only the items of that array that fit certain criteria, into a new array.

How do you use underscore in JavaScript?

Adding Underscore to a Node. js modules using the CommonJS syntax: var _ = require('underscore'); Now we can use the object underscore (_) to operate on objects, arrays and functions.

Is underscore js still used?

Lodash and Underscore are great modern JavaScript utility libraries, and they are widely used by Front-end developers.

Can you use filter on an array of objects JavaScript?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.


2 Answers

  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);
like image 191
pimvdb Avatar answered Sep 21 '22 17:09

pimvdb


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.
like image 44
David Weldon Avatar answered Sep 22 '22 17:09

David Weldon