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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:04:09+00:00 2026-06-14T11:04:09+00:00

I am getting back a JSON data from an API and I want to

  • 0

I am getting back a JSON data from an API and I want to compute the total cost for all the ‘cost’ field in the InvestmentSummary.InvestmentDetails array.
I am getting the following error:

Error: Unable to parse bindings.
Message: SyntaxError: invalid object initializer;
Bindings value: totalCost

My viewModel code looks like this:

        var init = { InvestmentSummary: { InvestmentDetails: [] } };
        ko.mapping.fromJS(init, {}, self);

        self.totalCost = ko.computed({
            read: function () {
                var result = 0;
                ko.utils.arrayForEach(self.InvestmentSummary.InvestmentDetails, function () {
                    result += item.Cost();
                });
                return result;
            }
        },self); 

        self.createBtnClick = function () {
            self.layoutView(true);
            var investAPI = "some_api";
            $.getJSON(investAPI, function (result) {
                ko.mapping.fromJS(result, {}, self);   
            });
        };

My JSON data looks like:

{
   InvestmentSummary: {
     InvestmentDetails: [
     {
        Cost: 0
     },
     {
        Cost: 1
     }]
   }
}

What am I doing wrong? Thanks

  • 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-14T11:04:10+00:00Added an answer on June 14, 2026 at 11:04 am

    Hard to tell without your full structure, but here are a couple of things that you would want to update:

    • InvestmentDetails will be an observableArray after going through mapping, so in your arrayForEach you will want the first argument to be self.InvestmentSummary.InvestmentDetails(), so that you can loop on the underlying array.

    • in the function being run on each detail, you are accessing item.Cost(), you will need to pass item into that function as the only argument.

    • when you are updating the data with the mapping plugin, you can omit the second argument (mapping options), if the structure has already been created.

    Here is a fiddle with these updates: http://jsfiddle.net/rniemeyer/6dqfC/

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

Sidebar

Related Questions

I am getting json data as a response back from the server. I want
I am submitting some form data via ajax and getting back a JSON array
Let say I'm getting back a JSON nested hash (or array of hashes) from
I'm retrieving the elevation data from the Google Maps API by AJAX. I'm getting
I am getting data back from my C# WebMethod. I'm not sure how to
I'm getting JSON back in this format from my C# webmethod: {d:[ADAMS CITY,BOULDER,ANTON,ARBOLES]} I
I'm having trouble handling a JSON object that I'm getting back from an AJAX
I am using jQuery to get back some JSON data from the server. I
I'm trying to return simple Json data back from a standard WebForm. It sounds
I'm getting the following (str) back from Disqus, JSON expected: ?({code: 0, response: {username:

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.