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

  • Home
  • SEARCH
  • 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 8439791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:05:58+00:00 2026-06-10T08:05:58+00:00

Actually the question is related to typeahead bootstrap because I need to define an

  • 0

Actually the question is related to typeahead bootstrap
because I need to define an array of values to show in input text by using autocomplete.

Anyway, the goal is just to define a function which read an array of objects and return an array of string.
Here is my code(1).

The goal of (1) is:
1) get an array of strings from an array of objects.
2) filter this array rejecting some elements.

It does not work because the element I want to reject persists in the array.
In fact in the autocomplete I get false value, actually it brokes the code because false is not expected by typeahead.

How should fix the code and maybe improve it?


(1)

element.typeahead({
    source: function ( {
        var users = _.map(app.userCollection.models, function (model) {
            if (model.get('id') === app.currentUser.id) {
                return false;
            }
            return model.get('first_name') + ' ' + model.get('last_name');
        });
        console.log(users); // [false, 'some name'];
        _.reject(users, function(name) {
            return name  === false;
        });
        console.log(users); // [false, 'some name'];
                            // why does the false value persist?
        return users;
    }
});
  • 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-10T08:06:00+00:00Added an answer on June 10, 2026 at 8:06 am

    Underscore methods don’t usually operate on the array itself, but they return a new array, but I suggest checking the underscore docs on each function individually for confirmation. In this case we can safely assume that reject returns some new array, according to this sentence in the underscore docs:

    Returns the values in list without the elements that the truth test (iterator) passes.

    What you are currently doing is:

    _.reject(users, function(name) {
      return name  === false;
    });
    

    So, you don’t actually save your results anywhere. To preserve a reference to the array without the unwanted elements do this:

    users = _.reject(users, function(name) {
      return name  === false;
    });
    

    That would yield the result you want, but let me give you a refactoring hint:

    Use backbone’s own methods as far as you can, it’ll make for more readable code

    source: function() {
      // filter the collection down to the users you want
      var users = app.userCollection.filter(function(model) {
        return model.id === app.currentUser.id;
      });
      // -> users is now an array of models
    
      // use map to transform each wanted user to a string giving its full name
      users = _.map(users, function(user) {
        return user.get('first_name')+' '+user.get('last_name');
      });
      // -> users is now an array of strings
    
      return users;
    }
    

    Hope this helps!

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

Sidebar

Related Questions

This is actually related to another question I had that was already answered. That
Actually I don't know whether my question is related to this thread. I want
This question is related to Hibernate using JPA (annotated Entities) and liquibase . I
I've already asked a related question, but sadly the answers, whilst correct, didn't actually
related question with unsatisfactory answers: Changing CSS Values with Javascript Here's what I want
Actually I have two question related to storyboard, Whether I can reuse my xib
Actually the question is in the title. Anyway I would like to repeat it:
Actually my question here is: are null and nil equivalent or not? I have
My question actually consists of multiple questions. I'm frequently reading about companies who deploy
The question actually in it. Google Calendar provides a convenient API for access to

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.