I'm looking to arrange my arrays by using the split function and then go through multiple items in those arrays.
Here's an example of an array structure:
var arr = [
{
"name": "bob",
"date": "1-1-2018",
"statistic": [
"some title: 92nd (source, 2014)",
"another title: 2.56 (source, 2014)",
"title: 52.8% women (source, 2007/08)",
"some title: 21.9% (source, 2016)",
"another title: 3rd (source, 2016)"
]
},
{
"name": "sally",
"date": "1-1-2020",
"statistic": [
"title: 8th (source, 2014)",
"some title: 92nd (source, 2014)",
"another: 40.8% women (source, 2007/08)",
"some title: 21.9% (source, 2016)",
"another title: 3 children (source, 2016)",
"some title: 23rd (source, 2016)"
"title: 46% (source, 2016)"
]
},
{
"name": "chris",
"date": "1-1-2021",
"statistic": [
"some title: 46th (source, 2014)",
"another title: 92nd (source, 2014)",
"title: 52.8% women/children (source, 2007/08)"
]
},
//etc...
]
This is what I have attempted so far:
for (let i=0; i < arr.length; i++) {
for (let x=0; x < arr[i].statistic.length; x++) {
arr[i].custom = {};
arr[i].custom["statistics"] = [];
var s = arr[i].statistic[x].split("(");
console.log(unit);
var l = s[0].split(":");
var u = l[1].split(" ");
arr[i].custom["statistics"].push({
number: u[1],
suffix: u[0],
label: l[0],
source: s[1]
});
}
}
How can I format my code like this?
var arr = [
{
"name": "bob",
"date": "1-1-2018",
"statistic": [
"some title: 92nd (source, 2014)",
"another title: 2.56 (source, 2014)",
"title: 52.8% women (source, 2007/08)",
"some title: 21.9% (source, 2016)",
"another title: 3rd (source, 2016)"
],
"custom": {
"statistics": [
{
"number": "92nd",
"suffix": "",
"label": "some title",
"source": "source, 2014)"
},
{
"number": "2.56",
"suffix": "",
"label": "another title",
"source": "CIA, 2017)"
},
{
"number": "52.8%",
"suffix": "women",
"label": "title",
"source": "source, 2007/08)"
},
{
"number": "21.9%",
"suffix": "",
"label": "some title",
"source": "source, 2016)"
},
{
"number": "3rd",
"suffix": "",
"label": "another title",
"source": "source, 2016)"
}
],
etc...
}
},
]
The issue is that "arr[i].statistic[x]" returns undefined even though there are values in the array. I will address the parenthesis problem later on to make it "source": "(source, 2014)"
Thank you for your help!