سؤال

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

هل كانت مفيدة؟

المحلول

data.agent_id = agentID;

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

نصائح أخرى

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top