Reputation: 5150
I deleted my old post as it contained a lot of spam. And I created a jsfiddle to reproduce the issue I am having.
console.clear();
var args = '{ action: downline }';
showObject(args);
showObject({ action: 'downline' });
function showObject(args) {
console.log(args);
}
How do I pass args as an object? I'm currently building args into a string dynamically and need to pass it as an object for the function to work correctly.
Upvotes: 1
Views: 91
Reputation: 571
Jquery "parseJSON" will help you.
$(document).ready(function() {
var args = $.parseJSON('{"action": "downline" }');
showObject(args);
showObject({
action: 'downline'
});
function showObject(args) {
console.log(args);
}
});
Upvotes: 0
Reputation: 95023
If you generated it as valid JSON
, you could parse the string as json and display that.
console.clear();
var args = '{ "action": "downline" }';
showObject($.parseJSON(args));
showObject({ action: 'downline' });
function showObject(args) {
console.log(args);
}
Upvotes: 0
Reputation: 146302
I am not sure what the issue is.
Your second way is how you pass an object to a function.
This fiddle shows the log: http://jsfiddle.net/maniator/mLxSP/1/
Upvotes: 0
Reputation: 1507
It is not your passing, but rather your declaration that is the problem. Define args as
var args = { action: 'downline' };
Upvotes: 2
Reputation: 3169
args.action, or args[action] will show downline
you can make also your object like: args = {}; args.action = 'downline'; etc
What's the problem?
Upvotes: 0