Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:46:12+00:00 2026-06-13T12:46:12+00:00

There has to be something simple I am missing here. http://jsfiddle.net/v9mdZ/ I am just

  • 0

There has to be something simple I am missing here.

http://jsfiddle.net/v9mdZ/

I am just learning Backbone.js and Underscore.js/Lodash and am trying to get familiar with chain.

I have the following code, which works as expected:

var ids = _.pluck(collection.where({'is_checked':true}), 'id');

I attempted to refactor this, using chain like so:

var ids = collection.chain().where({'is_checked':true}).pluck('id').value();

Why doesn’t the refactored code work? Am I using chain wrong?


Solution (details below)

Don’t use where with chain.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:46:13+00:00Added an answer on June 13, 2026 at 12:46 pm

    The merging of some Underscore methods into collections is a little imperfect. When you say collection.some_mixed_in_underscore_method(), the collection unwraps some of the Backbone stuff behind your back so that the Underscore method is applied to the attributes inside the collection’s models; it sort of works like this:

    var ary = _(this.models).map(function(m) { return m.attributes });
    return _(ary).some_mixed_in_underscore_method();
    

    But collection.chain() doesn’t work like that, chain just wraps the collection’s models directly so if you do this:

    console.log(collection.chain());
    

    you’ll see that chain is giving you an object that wraps an array of models. Your models won’t have an is_checked property (i.e. there is no model.is_checked), they will have is_checked attributes though (i.e. there will be model.get('is_checked') and model.attributes.is_checked).

    Now we can see where everything goes wrong:

    collection.chain().where({'is_checked':true})
    

    The models don’t have is_checked properties. In particular, there won’t be any models where is_checked is true and everything after the where is working with an empty array.

    Now that we know where things go sideways, how do we fix it? Well, you could use filter instead of where so that you can easily unpack the models:

    collection.chain()
              .filter(function(m) { return m.get('is_checked') })
              .pluck('id')
              .value();
    

    But, your models don’t have ids yet as you didn’t create them with ids and you haven’t talked to a server to get ids so you’re going to get an array of undefineds back. If you add some ids:

    var collection = new App.OptionCollection([
        {id: 1, 'is_checked': true},
        {id: 2, 'is_checked': true},
        {id: 3, 'is_checked': false}
    ]);
    

    then you’ll get the [1,2] that you’re looking for.

    Demo: http://jsfiddle.net/ambiguous/kRmaD/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There has to be something utterly simple I'm overlooking here, but for the life
Is there a way through the .Net aframework (or has someone written something similar)
The routing I need is quite simple, I must be missing something there. As
Has anyone used both Joone and Encog in production applications? Is there something you
There has GOT to be a simple way to programmatically render HTML into a
Sorry for the unlearned nature of this question. If there's a simple answer, just
There has to be a better way of writing the js in my function....
There has been a lot of talk around iPad-Apps / Approval / Store-related Questions.
There has always been a confusion with preg_match in php. I have a string
There has been similar questions asked (and answered), but never really together, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.