Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout.js updating array within an array
    primarykey
    data
    text
    <p>I have an array in Knockout view model which looks like so:</p> <pre><code>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" }, ]}, ]); </code></pre> <p>I use this code to add a new element to it:</p> <pre><code>this.Activities.push(new NewActivity(company, description, length, fullDate)); </code></pre> <p>Which uses NewActivity function:</p> <pre><code>function NewActivity(company, description, length, date) { this.date = date; this.activities = [{ "company": company, "description": description, "length": length }]; } </code></pre> <p>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.</p> <p><strong>How can I do it?</strong> </p> <p>All of the data for the Activities array comes from the model in the strongly typed view in MVC application:</p> <pre><code>this.Activities = ko.observableArray([ @foreach (ActivitySet aSet in Model) { @:{ "date": "@aSet.Date", @:"activities": [ foreach(Activity a in aSet.Activities) { @:{ "company": "@a.Companies.Select(c =&gt; c.Title).Single()", @:"description": "@a.Descriptions.Select(c =&gt; c.Title).Single()", @:"length": "@a.LengthInMinutes" @:}, } @:]}, } ]); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload