简体   繁体   中英

backbone.js collection where() function

I'm reading a book on rapid prototyping in JS. There is this line of code:

var appleModel = this.collection.where({name: appleName})[0];

I know where() returns matching models in the collection. But what does the [0] at the end do?

Backbone's .where() function returns an array of models. The [0] at the end simply takes the first model returned by .where() .

Backbone has its own method when it comes to searching for first result. collection.findWhere(attributes)

http://backbonejs.org/#Collection-findWhere

From my python experience this selects the first element of an array. Obviously where() returns a list (array).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM