Question

I am trying to add more data to my data object before passed in ajax call. I know push won't work because it is not an array. What would the alternative to push be for an object?

var data = { 
    street: $('#FullStreetAddress').val(), 
    city: $('#City').val(), 
    state: $('#State').val(),
    doc_name: []
};

data.push({
    agent_id: agentID,
});

Thanks

Was it helpful?

Solution

data.agent_id = agentID;

As you mentioned, push only works on an Array instance.

OTHER TIPS

Although you can use the property style syntax:

data.agent_id = agentID;

Using brackets is slightly more robust:

data['agent_id'] = agentID;

This is because any string can be used as a key in an object, but not as a property name. Eg:

data['agent.id'] = agentID; // works
data.agent.id = agentID; // error
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top