I have a json key value object that contains...
let myObject = {
"data[contentblocks][0][title]": "something",
"data[contentblocks][1][title]": "some other title",
"data[seo][description]": "some description",
"data[headerimage]": "something.jpg"
}
I'm trying to loop over the object and create an object that matches the square bracket structure...
let data = {
contentblocks: [
{title: "something"},
{title: "some other title"}
],
seo: { "description": "some description"}
headerimage: "something.jpg"
}
etc...
I tried looping over the object
for(let key in formData)
{
let finalValue = formData[key];
console.log(key, finalValue)
}
and was going to simply do an eval(key+ " = " + finalValue) but gives an undefined error. I'm wondering if there is a simple way to do this that I'm not entirely seeing. I'd rather do this with native javascript, but I do have jquery on the site as well.
I am considering doing a loop and exploding the keys, but I fear I'm going down a path that is more complex than I need.