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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:52:50+00:00 2026-05-31T12:52:50+00:00

I’m pretty new to Knockout.js and am facing now a situation where I’m not

  • 0

I’m pretty new to Knockout.js and am facing now a situation where I’m not really sure how to handle it right. The thing is: I got a bunch of objects that I’m retrieving via ajax. The result will be somewhat like this:

var Objects = [ { id: 0, name: "Foo", type: "A" },
                { id: 1, name: "Bar", type: "B" },
                { id: 1, name: "Bar", type: "A" }, ... ];

What I’ve done so far (simplified):

var ViewModel = function() {
    var self = this;
    self.objects = ko.observableArray(Objects);
};

Now I need to render those objects in different lists depending on their “type”. So there is a list of objects of type “A” and one for the objects of type “B” etc. (there are five types at the moment, but there may be added some more in the future).

I’ve come up with this (working) solution:

var ViewModel = function() {
    var self = this;
    self.objects = ko.observableArray(Objects);

    self.objectsA = ko.computed(function() {
        return ko.utils.arrayFilter(self.objects(), function(item) {
            return (item.type == 'A');
        });
    });

    self.objectsB = ...
    self.objectsC = ...
};

And in my actual view:

<h1>Type A</h1>
<ul class="typeA" data-bind="template: { name: 'object', foreach: objectsA }"></ul>

<h1>Type B</h1>
<ul class="typeB" data-bind="template: { name: 'object', foreach: objectsB }"></ul>

Is there a better way to address this problem? This works, but it is a bit ugly, not really dynamic and includes a lot of repetition.

  • 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-05-31T12:52:51+00:00Added an answer on May 31, 2026 at 12:52 pm

    Bindings are executed inside of a computed observable. This means that you can choose to use a simple function that takes a parameter rather than an actual computed observable if you would like.

    This means that you can simplify it down to:

    var ViewModel = function() {
        var self = this;
        self.objects = ko.observableArray(Objects);
    
        self.filterByType = function(type) {
            return ko.utils.arrayFilter(self.objects(), function(item) {
                return (item.type === type);
            });
        };
    };
    

    Then, bind against it like:

    <h1>Type A</h1>
    <ul class="typeA" data-bind="template: { name: 'object', foreach: filterByType('A') }"></ul>
    
    <h1>Type B</h1>
    <ul class="typeB" data-bind="template: { name: 'object', foreach: filterByType('B') }"></ul>
    

    Your UI will update now whenever the array is manipulated (items added/removed). However, if you are going to be editing the type on the fly, then type would need to be an observable for the computed observable to update (in your original method or in this way).

    Sample here: http://jsfiddle.net/rniemeyer/NFbxc/

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

Sidebar

Related Questions

I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
i got an object with contents of html markup in it, for example: string

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.