Created
May 25, 2023 01:30
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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