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 any solution to do this using underscore.js?Any ideas? Elegant solutions?
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 any solution to do this using underscore.js?Any ideas? Elegant solutions?
Share Improve this question asked Jan 16, 2016 at 7:28 SVKSVK 2,1972 gold badges21 silver badges41 bronze badges 2- 2 Sounds like an X/Y problem. What is producing this? Whatever produced it needs to be fixed – mplungjan Commented Jan 16, 2016 at 7:32
- Which fuction does create your array? – user5780762 Commented Jan 16, 2016 at 7:45
3 Answers
Reset to default 9You can use this
var updatedList = JSON.stringify(jsonList.list, function (key, value) {return (value === undefined) ? "" : value});
Demo Link Here
Apologies for the delay, if you don't care about modifying the existing array and its values then this will probably be a lot quicker performance wise. If you can do anything natively using vanilla js then this should always be used over any libraries in my opinion.
jsonList.list.forEach(function(obj) {
for(var i in obj) {
if(obj[i] === undefined) {
obj[i] = '';
}
}
});
You can see the latest version on jsbin here https://jsbin./xinuyi/3/edit?html,js,output
This is a very quick solution, I haven't benchmarked it but see the solution below or on jsbin: https://jsbin./xinuyi/2/edit?html,js,output
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 "
}]
};
var updatedList = _.map(jsonList.list, function(object, index) {
return _.mapObject(object, function(val, key) {
return (val === undefined) ? "" : val;
});
});