pmestima
pmestima

Reputation: 119

Backbone.js collection where

I'm triyng to do a filter on a collection, the model is a Student like this:

{
   code: "some code",
   name: "some name",
   course: {
      course_code: "some code",
      course_name: "course name"
   }
}

if I try to do a filter like this:

var myVar = students.where({code: "some code"})

myVar will be filled with students according to the code and there are no problems. But how can I do filter by course_code? I already tried:

var myVar = students.where({course: {course_code: "some code"}})

but I get nothing, if I try

var myVar = students.where({course.course_code: "some code"}) 

I get error.

Upvotes: 0

Views: 649

Answers (2)

HungryCoder
HungryCoder

Reputation: 7616

let's assume you are finding students with course code 101.

var student_with_course_code = students.filter(function(student) {
      return student.get('course').get('code') == 101;
});

Upvotes: 3

neeebzz
neeebzz

Reputation: 11538

var myVar = students.find(function(s) {return s.course.course_code == "some code"} ) 

Upvotes: 1

Related Questions