Andy
Andy

Reputation: 19251

Get backbone model from array of objects

Hi I have a collection response from backbonejs which is access like

this.collection.models

and returns an array of length 7 - each with a model inside it.

I am trying to search across this array and get the model which matches

_id: Xmas

But everytime I try to do it I get the error

Object [object Array] has no method 'get' 

Can anyone help!?

Upvotes: 0

Views: 344

Answers (2)

d4kris
d4kris

Reputation: 538

You should be working with the collection and not its inner array models, then you can use the underscore methods proxied in collection, for example:

var xmasModel;
xmasModel = this.collection.find(function(item) {
  return item.get("_id") == "Xmas"
})

Upvotes: 0

muneebShabbir
muneebShabbir

Reputation: 2528

you can try this for iterating and getting value

_(collection.models).each(function(t){ 
                //console.log("loop Model"+t)
                t.get("title")
enter your required logic here
            }, this);

Upvotes: 1

Related Questions