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 9225347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:33:15+00:00 2026-06-18T04:33:15+00:00

I have an array of objects that a user can perform search on. I’m

  • 0

I have an array of objects that a user can perform search on. I’m using a ko.computed function based on the search to create another array of matched items for display.

self.matchedRecords = ko.computed(function() {
  return ko.utils.arrayFilter(self.transponders(), function(r) {
    return r.title.toLowerCase().indexOf($('.search-input').val().toLowerCase()) > -1
  }
};

This works great and I’m really impressed with the performance so far.

This issue is I also need the “unmatched” records because I have to perform an addition operation on them in some cases (60% of the time). I don’t really want to create a second ko.computed function because then I have to run through this array a second time every time a search is performed.

So, my question: Is there a way that I can use the same ko.computed to create a second array of unmatched items? Basically run through the array and put each item in the matched or unmatched array…

If not, is it faster to:
1) create a second ko.computed to get the unmatched items from my array as the user searches; or
2) write an arrayDiff function and determine the unmatched items on demand if I need them.

Cheers!

  • 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-18T04:33:16+00:00Added an answer on June 18, 2026 at 4:33 am

    If you are worried about performance, you could have a non-observable array that you populate while you iterate the search results in your computed. Also note that you are repeatedly selecting using jQuery inside your loop, which I think negates any KO-caused slowdowns.

    self.missedRecords = [];
    
    self.matchedRecords = ko.computed(function() {
        var searchQuery = $('.search-input').val().toLowerCase(),
            transponders = self.transponders(),
            matched = [];
    
        // Clear out missed records
        self.missedRecords.length = 0;
    
        _.each(transponders, function(transponder) {
            if (transponder.title.toLowerCase().indexOf(searchQuery) >= 0) {
                matched.push(transponder);
            } else {
                self.missedRecords.push(transponder);
            }
        });
    
        return matched;
    });
    

    I used _.each from Underscore to keep the code shorter. The drawback of this approach is that changes to missedRecords can’t (reliably) be bound to the UI (e.g. in case you have a foreach binding).

    If you do need the missedRecords array to be observable, and still want to keep things fast(er), you could do something like this:

    self.missedRecords = ko.observableArray([]);
    
    self.matchedRecords = ko.computed(function() {
        var searchQuery = $('.search-input').val().toLowerCase(),
            transponders = self.transponders(),
            matched = [],
            missed = [];
    
        _.each(transponders, function(transponder) {
            if (transponder.title.toLowerCase().indexOf(searchQuery) >= 0) {
                matched.push(transponder);
            } else {
                missed.push(transponder);
            }
        });
    
        // Clear out missed records, without triggering subscriptions
        self.missedRecords().length = 0;
    
        // Copy the local missed array to the KO observable array
        // This will NOT trigger notifications
        ko.utils.arrayPushAll(self.missedRecords(), missed);
    
        // Tell KO that the observable array has mutated - this will trigger changes
        // to anything observing the missedRecords array
        self.missedRecords.valueHasMutated();
    
        return matched;
    });
    

    You could also skip computed altogether and just subscribe to changes to change the state of your arrays. For example:

    self.missedRecords = ko.observableArray([]);
    self.matchedRecords = ko.observableArray([]);
    
    self.transponders.subscribe(function(newTransponders) {
        var matched = [],
            missed = [];
    
        _.each(newTransponders, function(transponder) {
            // Populate matched/missed local arrays
        });
    
        // Copy the arrays to the observableArray instances using the technique above
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that takes an array of objects and I need to
I have a set of objects that the user can sort arbitrarily. I would
I have an array that contains objects from two models: @search_results = User.find(:all, :conditions
I have an array of objects that have QTTime structs as attributes. The objects
I have a figure below: And my question is that the objects in Array
I have a mutable array that has about 100 objects in it and each
I have a form that generates an array of attribute objects. The attribute objects
I have a mutable array that is retained and storing several objects. At some
I have a method that is adding custom objects to an array in a
I have the following sort descriptors that sort an array of my business objects,

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.