I have an array of objects that I need to convert into strings for a CSV parsing program. The array looks like this:
var arr = [{request: {funding : 123, id: 123abc, membership: true},
response: {funding : 285, success: true }},
{request: {funding : 123, id: 123def, membership: true},
response: {funding : 167, success: true }},
{request: {funding : 123, id: 123def, membership: true},
response: {funding : 234, success: true }}]
I tried using a loop to convert the nested objects into strings using JSON.stringify(), but it didn't seem to work as expected:
for (var item in arr)
{ item.response = JSON.stringify(item.response);
item.request = JSON.stringify(item.request);
}
Even after running this code, checking the type of item.response
still returns object
.
However, when manually setting the property of an individual item outside of the loop, it does work correctly:
arr[0].response = JSON.stringify(arr[0].response)
typeof(arr[0].response) // string