I have a JSON data set that looks like this:
{"id":1,"name":"Sam","birthday":"12December","age":"15"},
{"id":2,"name":"Ash","birthday":"12January","age":"23"}
After passing the data through the function:
ConvertToCSV(data)
I can extract id, name, birthday, and age in the csv output. However, I want to exclude the birthday column from the conversion process to prevent it from appearing in the generated csv. How can I achieve this?
Current Output:
id | name | birthday | age
1 | Sam | 12December | 15
2 | Ash | 12January | 23
Desired Output:
id | name | age
1 | Sam | 15
2 | Ash | 23
Below is the original code snippet:
ConvertToCSV(data) {
var array = $.parseJSON('[' + data + ']');
var fields = Object.keys(array[0]);
var replacer = function(key, value) { return value === null ? '' : value }
var csv = array.map(function(row){
return fields.map(function(fieldname){
return JSON.stringify(row[fieldname], replacer)
}).join(',')
})
csv.unshift(fields.join(',')) // add header column
var str = csv.join('\r\n');
return str;
}
I tried to modify the function by adding the following code based on this reference:
for(var i = array.length - 1; i >= 0; i--) {
var index = array[i].birthday;
array[i].splice(index, 1);
}
However, the above code snippet does not produce the desired outcome. Can anyone assist me with this? Thank you!