0

I have an Object inside AJAX call and I'm not able to add new property to its property containing another object.

Here is my attempt:

$.post('/home/save.json', {obj:obj}, function(data) {
    var qu = {};
    qu["Id"] = "some value";
    qu["Type"] = "some value";
    qu["Name"] = "some value"
    qu["Variants"] = data["Variants"]
    if (data['Subvariant']) { 
        qu["Variants"]["Subvariant"] = data['Subvariant'] 
    }
    // ...
});
1
  • Are you sure that data["Variants"] is actually an object? What does the relevant part of your data (the JSON) look like? What you have will work as long as data["Variants"] is an object. Commented Oct 11, 2012 at 9:53

1 Answer 1

1

You're missing some semicolons, and I would use dot notation

var qu = {};
qu.Id = "some value";
qu.Type = "some value";
qu.Name = "some value";
qu.Variants = data.Variants;
if (data.Subvariant !== undefined && data.Subvariant !== null) {
    qu.Variants.Subvariant = data.Subvariant;
}

Or are you maybe meaning to check data.Variants.Subvariantinstead of data.Subvariant?

Sign up to request clarification or add additional context in comments.

1 Comment

@Alega: There is no difference between dot and bracket notation. They are equivalent. It makes no sense that this code works but yours does not. You must have changed something else as well.

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.