문제

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