0

I have an object with nested arrays which I:

  1. First turn into string using angular.toJSON() as ngCSV doesn't support nested arrays
  2. Then export as CSV file using ngCSV
  3. Then import and parse into JSON again using PapaParse.

The initial array looks like this:

filteredRecords [
{
date: '', 
time: '', 
comments: [
    {message: '',
    commenttime: ''},
    {message: '',
    commenttime: ''},
    {message: '',
    commenttime: ''}
],
arrival: '',
},
{
date: '', 
time: '', 
comments: [
    {message: '',
    commenttime: ''},
    {message: '',
    commenttime: ''},
    {message: '',
    commenttime: ''}
],
arrival: '',
}
]

After I've used angular.toJson() and done the export/import, the comment property looks like this:

filteredRecords [
{
date: '', 
time: '', 
comments: "[{"message":"wopwop","commenttime":"10.46"},{"message":"checkingin","commenttime":"10.46"},{"message":"tripletest","commenttime":"10.46"}]",
arrival: '',
}, ...
]

My problem is that I need to reverse the process and turn this string into an array after it's parsed, as my app needs to be able to read comment.message and comment.commenttime.

Any idea on how to proceed?

EDIT

I've managed to manually parse the string back into array using the solution from lex82, but I don't know how I could iterate into the main array and automatically do the same for all comments objects in every object from the array.

Any tips?

Eric Mitjans
  • 2,149
  • 5
  • 40
  • 69

1 Answers1

1

It looks like you need JSON.parse(). I think this should work:

var yourArray = JSON.parse(yourString);

You find a more elaborate answer here.

Community
  • 1
  • 1
lex82
  • 11,173
  • 2
  • 44
  • 69
  • lex82, it works but only manually! How could I automatize it for every **comments** object of every object in the array? – Eric Mitjans Aug 30 '15 at 15:27
  • I'm not sure if I understand your question. What do you mean with manually? Can you post some code how you do it manually? Can't you just loop over the array and do it for every element? – lex82 Aug 30 '15 at 18:19