I've this object:
[
{
"_token": "lRM32nH7KAnt2xdDkUJBJYniNnANJVhG20BGnjHE",
"academic[2][id]": "-1",
"title[2][name]": "Test Title",
"from_date[2]": "2021-05-16",
"to_date[2]": "2021-05-17",
"institute[2]": "Titletest title test title ",
"title[3][name]": "Test TitleTest Title",
"from_date[3]": "2021-05-17",
"to_date[3]": "2021-05-18",
"institute[3]": "test title test title test title test title "
}
]
And i want to restructure it to:
[
{"title": "Test Title", "from_date": "2021-05-17", "to_date": "2021-05-18","institute":"Title"},
{"title": "Test TitleTest Title", "from_date": "2021-05-17", "to_date": "2021-05-18","institute":"Title"},
{"title": "Test Title", "from_date": "2021-05-17", "to_date": "2021-05-18","institute":"Title"},
]
How can i do this using javascript? or any simple approach using javascript?
Edit: What I've done so far is:
const data = new FormData(document.querySelector('#academic-form'));
const result = [Object.fromEntries(data.entries())][0];
const academics = [];
for(var key in result){
// console.log('key: ' + key);
console.log('title: ' + result[key]);
console.log(result[i]);
academics.push({
//push values in academics array.
});
}
const result = data.map(item => ({ title: item['title[2][name]'], <othervalues> }));, and then just replace<othervalues>with the additional properties (similar to title)?title[2][name]is just for demo there are multiple values which can range from 2 to 10 or 15. What to do in that case?