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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:17:45+00:00 2026-06-05T16:17:45+00:00

SuperNoob here, trying to load varying multiple Options either as a string[] or object,

  • 0

SuperNoob here, trying to load varying multiple “Options” either as a string[] or object, to a “Product” that can be added to a shopping cart using Knockout: (working from John Papa’s PluralSight Knockout Change Tracking example)
Here is the view:

<div>
    <span>OptionsArray: </span>
    <ul data-bind="foreach: optionsArray">
        <li data-bind="text: $data"></li>
    </ul>
</div>

Here is the JSON converted into C#:

{Id = 4, ModelId = 1, SalePrice = 1.00, ListPrice = 1.00, Rating = 5, Photo = "smallCoffee.jpg", CategoryId = 1, ItemNumber = "smallCoffee", Description = "Small Coffee", Model = new Model(){ Name = "Small Coffee", Brand = "Tim Hortons", Id = 1 }, Category = drinkCat, Options = new Options(){Name = "Sugar"}, OptionsArray = new string[]{"Sugar", "Cream"}}

This javascript function works, loading json into an “products” array of Products, except I can’t get it to iterate through varying multiple options for each product.
Here is the function part of the view model:

loadProductsCallback = function (json) {
            my.vm.tracker().markCurrentStateAsClean();
            $.each(json, function (i, p) {
                products.push(new my.Product(selectedProduct)                            
                        .id(p.Id)
                        .salePrice(p.SalePrice)
                        .photo(p.Photo)
                        .category(new my.Category()
                        .id(p.Category.Id)
                        .name(p.Category.Name)
                            )
                        .model(new my.Model()
                        .id(p.Model.Id)
                        .name(p.Model.Name)
                        .brand(p.Model.Brand)
                            )
                        .options(new my.Options()
                        .name(p.Options.Name)
                            )                            
                        .description(p.Description)
                        .rating(p.Rating)
                        .stateHasChanged(false);
                        $.each(p.OptionsArray, function(i, o) { p.optionsArray.push(o);});
                );
            });

And here is the Product object that is taking all the data:

Product = function (selectedItem) {
    var self = this;
    self.id = ko.observable();
    self.salePrice = ko.observable();
    self.photo = ko.observable();
    self.model = ko.observable();
    self.options = ko.observable();
    self.optionsArray = ko.observableArray(["1", "2"]);
    self.category = ko.observable();
    self.description = ko.observable();
    self.rating = ko.observable();
    self.isSelected = ko.computed(function () {
        return selectedItem() === self;
    });
    self.isDrink = ko.computed(function () {
        return this.category() ? this.category().id() === 1 : false;
    }, self),
    self.isFood = ko.computed(function () {
        return this.category() ? this.category().id() === 4 : false;
    }, self),

    self.shortDesc = ko.computed(function () {
        return this.model() ? this.model().name() : "";
    }, self),
    self.opt = ko.computed(function () {
        return this.options() ? this.options().name() : "";
    }, self),
    self.optQty = ko.computed(function () {
        return this.options() ? this.options().qty() : "";
    }, self),
    self.photoUrl = ko.computed(function () {
        return photoPath + this.photo();
    }, self),
    self.rating.subscribe(function () {
        this.stateHasChanged(true);
    }, self),
    self.stateHasChanged = ko.observable(false);
};

The loadProductsCallback function works fine except for the part adding the nested OptionsArray in. I’ve tried a nested $.each functions and just simply .optionsArray(p.OptionsArray).

There is a lot more code: I hope that this is enough to make sense.

Dear Stackers, how can I properly load the OptionsArray into the Products object using this function?

  • 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-05T16:17:47+00:00Added an answer on June 5, 2026 at 4:17 pm

    Not sure I’m quite understanding your code but here goes.

    You are new’ing up a product then trying to loop through a collection and map it to options. Except you are pushing the results to the source object.

    What about this.

    loadProductsCallback = function (json) {
        my.vm.tracker().markCurrentStateAsClean();
        $.each(json, function (i, p) {
            var newProduct = new my.Product(selectedProduct)                            
                    .id(p.Id)
                    .salePrice(p.SalePrice)
                    .photo(p.Photo)
                    .category(new my.Category()
                    .id(p.Category.Id)
                    .name(p.Category.Name)
                        )
                    .model(new my.Model()
                    .id(p.Model.Id)
                    .name(p.Model.Name)
                    .brand(p.Model.Brand)
                        )
                    .options(new my.Options()
                    .name(p.Options.Name)
                        )                            
                    .description(p.Description)
                    .rating(p.Rating)
                    .stateHasChanged(false);
            $.each(p.OptionsArray, function(i, o) { 
                newProduct.optionsArray.push(o);
            });
    
            products.push(newProduct);
        });
    }
    

    Hope this helps.

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

Sidebar

Related Questions

Super noob question here... The file source: www.mysite.com/blah/blah/customdir/ajax/folder/file.php and I want to include a
I have a Rails app that uses MySQL, MongoDB, NodeJS (and SocketIO). Right now,
OLD : private string Check_long(string input) { input = input.Replace(cool, supercool); input = input.Replace(cool1,
I am trying to write to an xml file in a certain structure and
I am a regex supernoob (just reading my first articles about them), and at
I'm trying to save the kABFirstNamePropert, kABLastNameProperty and kABAddressProperty all saved into an array
I'm trying to do a simple implementation of the Specification pattern in my domain
Lets say that I have an array that I want to convert to a
I'm trying to learn some python and I wanted for python to interact with
I have a php script validation that looks for some special chars and deletes

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.