Skip to content

Instantly share code, notes, and snippets.

@BruceMcKinnon
Created May 25, 2023 01:30
Show Gist options
  • Save BruceMcKinnon/d0ac5cf388b7599563396b45861d94cc to your computer and use it in GitHub Desktop.
Save BruceMcKinnon/d0ac5cf388b7599563396b45861d94cc to your computer and use it in GitHub Desktop.
JS - Convert multi-dim array that has been JSON.stringify to a string back to a JS multidim array
//
// Take a string like '{"title":"An event","start":"2023-05-01"},{"title":"Another event","start":"2023-05-15"},{"title":"Yet another event","start":"2023-05-24"}'
// and convert it back into a multidim array
var string_vers = JSON.stringify(original_array);
var reconstructed = reconstruct_multi_dim( string_vers );
console.log( JSON.stringify(reconstructed) );
function reconstruct_multi_dim ( events ) {
var multi_dim = [];
var pieces = events.split('},{');
for (var idx=0; idx < pieces.length; idx++) {
// Fix up the formatting of the individual JSON stringified arrays
pieces[idx] = '{'+pieces[idx]+'}';
pieces[idx] = pieces[idx].replace("{{", "{");
pieces[idx] = pieces[idx].replace("}}", "}");
var new_bit = JSON.parse(pieces[idx]);
multi_dim = multi_dim.concat( new_bit );
}
return multi_dim;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment