javascript - How to replace undefined values with empty string in an array object? -


array object:

 var jsonlist= {         "list": [{             "column_name": "control_master_id",             "referenced_column_name": "control_master_id",             "referenced_table_name": "tbi_controls_master",             "table_name": "tbi_widget_controls"         }, {             "column_name": "authorization_id",             "referenced_column_name": "authorization_id",             "referenced_table_name": "tbi_authorization_master",             "table_name": "tbi_controls_master"         }, {             "column_name": undefined,             "referenced_column_name": undefined,             "referenced_table_name": undefined,             "table_name": "tbi_widget_controls "         }]     } 

expected solution:

var jsonlist={     "list": [{         "column_name": "control_master_id",         "referenced_column_name": "control_master_id",         "referenced_table_name": "tbi_controls_master",         "table_name": "tbi_widget_controls"     }, {         "column_name": "authorization_id",         "referenced_column_name": "authorization_id",         "referenced_table_name": "tbi_authorization_master",         "table_name": "tbi_controls_master"     }, {         "column_name": "",         "referenced_column_name": "",         "referenced_table_name": "",         "table_name": "tbi_widget_controls "     }] } 

is there solution using underscore.js?any ideas? elegant solutions?

you can use this

var updatedlist = json.stringify(jsonlist.list, function (key, value) {return (value === undefined) ? "" : value}); 

demo link here


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -