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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:32:08+00:00 2026-06-15T05:32:08+00:00

I have an array in Knockout view model which looks like so: this.Activities =

  • 0

I have an array in Knockout view model which looks like so:

this.Activities = ko.observableArray([
{ "date": "28/11/2012 00:00:00",
  "activities": [
     { "company": "BOW",
       "description": "Backup Checks",
       "length": "60"
     },
     { "company": "AMS",
       "description": "Data Request",
       "length": "135"
     },
  ]},
{ "date": "30/11/2012 00:00:00",
  "activities": [
     { "company": "BOW",
       "description": "Backup Checks",
       "length": "60"
     },
     { "company": "SLGT",
       "description": "Software Development",
       "length": "240"
     },
     { "company": "BOW",
       "description": "Data Request",
       "length": "30"
     },
  ]},
]);

I use this code to add a new element to it:

this.Activities.push(new NewActivity(company, description, length, fullDate));

Which uses NewActivity function:

function NewActivity(company, description, length, date) {
    this.date = date;
    this.activities = [{ "company": company, "description": description, "length": length }];
}

And it works fine. However, it creates an entirely new object every time it is getting released. I need to implement a condition when the code would check for the date of the objects already created. If the newly created object had the same date, activity details should be added to activities array within the Activities array for that date.

How can I do it?

All of the data for the Activities array comes from the model in the strongly typed view in MVC application:

this.Activities = ko.observableArray([
        @foreach (ActivitySet aSet in Model)
        {
            @:{ "date": "@aSet.Date",
            @:"activities": [
                 foreach(Activity a in aSet.Activities)
                 {
                     @:{ "company": "@a.Companies.Select(c => c.Title).Single()",
                       @:"description": "@a.Descriptions.Select(c => c.Title).Single()",
                       @:"length": "@a.LengthInMinutes"
                     @:},
                 }
            @:]},
        }
]);
  • 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-15T05:32:10+00:00Added an answer on June 15, 2026 at 5:32 am

    I suggest that you create a few entities describing your activities:

    // Details
    function ActivityDetails(company, description, length) {
        this.company = ko.observable(company);
        this.description = ko.observable(description);
        this.length = ko.observable(length);
    }
    
    // Activity
    function Activity(date, activityDetails) {
        this.date = ko.observable(date);
        this.details = ko.observableArray(activityDetails);
    }
    

    The you can control activities in the following manner:

    function ViewModel () {
        var self = this;
    
        this.Activities = ko.observableArray([
            // Activities here
        ]);
    
        this.addActivity = function (activity) {
            var flag = false;
            ko.utils.arrayMap(self.Activities(), function (item) {
                // Flag is here so it doesn't keep checking further in iterations
                if (!flag) {
                    if (item.date() === activity.date()) {
                        item.details.push(activity.details);
                        flag = true;
                    }
                }
            });
            // Case where activity date was not found in existing records
            if (!flag) {
                self.Activities.push(activity);
            }
        }
    }
    

    This requires your view model to have a custom add method which I have provided an example of. Note that everywhere I am resolving observable values, so if you are using non-observable ones remove the function calls.

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

Sidebar

Related Questions

I have the following view model which contains an array of elements function ReservationsViewModel()
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have array object in jquery. array=[]; array{Id:user.Id,Date:user.Date}; Now am going to pass this
i have array like below which is sorted by array_count_values function, Array ( [Session
I am using Ryan Niemeyer`s Knockout-Kendo.js library. I have a kendodropdown defined like this:
I have a view model setup with an observable array of user objects. The
I have Knockout.js view model that has nested observable arrays. function ParentVM(data) { var
I have array like this: array( array( 'id' => 1, 'children' => array( array(
I have array like below the page. I want to find array number which

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.