0

I would like to insert data to a nested array. The structure of the JSON object is like this:

Data: {
    MembershipName:
    Countries: {
                CountryName: "USA"
                States:
                                {
                                  State: "Alabama"
                                       {
                                          Members: {}
                                       } 
                                }
       }
}

I tried using the push function below, it's being added when I debug it in the console but my View isn't updating.

self.Data()[0]["Countries"].push(
            {
            "CountryName": 'Success!'
            }
        );

I would like to add to the Countries array specifically for this specific index (I used index for 0). Is there an efficient way to achieve this?

2
  • Hi @Ricalyn, not really sure what this has to do knockoutjs. Could you please add some more detail and enough code to help us understand what you are trying to do. Commented Jul 11, 2019 at 6:54
  • Hi @NathanFisher updated. The self.push is the KnockOutJS related. I'm trying to push to a nested array. Commented Jul 11, 2019 at 7:43

2 Answers 2

1

Your data structure is not so clear, it seems that Data is an object (but you go through it with [0]) and countries is another object (but you want to push a new value like an array).

Maybe you thought about this data structure:

Data: {
    MembershipName: {},
    Countries: [
        {
            CountryName: "USA",
            States: [
                { 
                    State: "Alabama",
                    Members: []
                }
            ]
        }
    ]
}

In this case, you can push the new country with:

self.Data().Countries.push({ /* new country data */ });
Sign up to request clarification or add additional context in comments.

2 Comments

Arrays should be observableArray, is that it?
Hi @Bludev yes, I actually have some issues with my mapping, but I solved it as well, it was the main problem.
0

I have already solved this by doing the following:

self.Data()[index].Countries.push(
            new countriesModel(
                {
                    'CountryName': selectedCountry()
                }
            )
        );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.