使用JS使用split组织数组
organizing array using split using JS
我需要使用 split 函数组织我的数组并循环访问数组中的多个项目。
我有一个看起来像这样的数组
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...
]
这是我试过的
for (let i=0; i < arr.length; i++) {
for (let x=0; x < arr.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]
});
}
}
如何使我的代码看起来像这样?
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...
}
},
]
当我知道数组中有内容时,它告诉我“arr[i].statistic[x]”返回未定义。我稍后可以修复左括号并将其设为“来源”:“(来源,2014)”
提前致谢
您正在迭代外部数组两次,而不是迭代内部循环中的每个项目 statistic
。尝试使用
for (let i=0; i < arr.length; i++) {
arr[i].custom = {};
arr[i].custom["statistics"] = [];
for (let x=0; x < arr[i].statistic.length; x++) {
// … further code …
代替你的循环。
此外,由于我没有足够的声誉来发表评论,有一个问题:您是否故意记录一个从未定义过的变量 unit
?也许你可能想要记录 s
?
您的循环没有正确嵌套,您的第二个循环没有遍历正确的项目。您每次都在重新创建 [statistics] 数组,因此只会得到其中的最后一项。
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)"
]
}
]
for (let i = 0; i < arr.length; i++) {
arr[i].custom = {};
arr[i].custom["statistics"] = [];
for (let x = 0; x < arr[i].statistic.length; x++) {
var s = arr[i].statistic[x].split("(");
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].substr(0, s[1].length - 1)
});
}
}
console.log(arr)
我需要使用 split 函数组织我的数组并循环访问数组中的多个项目。
我有一个看起来像这样的数组
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...
]
这是我试过的
for (let i=0; i < arr.length; i++) {
for (let x=0; x < arr.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]
});
}
}
如何使我的代码看起来像这样?
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...
}
},
]
当我知道数组中有内容时,它告诉我“arr[i].statistic[x]”返回未定义。我稍后可以修复左括号并将其设为“来源”:“(来源,2014)”
提前致谢
您正在迭代外部数组两次,而不是迭代内部循环中的每个项目 statistic
。尝试使用
for (let i=0; i < arr.length; i++) {
arr[i].custom = {};
arr[i].custom["statistics"] = [];
for (let x=0; x < arr[i].statistic.length; x++) {
// … further code …
代替你的循环。
此外,由于我没有足够的声誉来发表评论,有一个问题:您是否故意记录一个从未定义过的变量 unit
?也许你可能想要记录 s
?
您的循环没有正确嵌套,您的第二个循环没有遍历正确的项目。您每次都在重新创建 [statistics] 数组,因此只会得到其中的最后一项。
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)"
]
}
]
for (let i = 0; i < arr.length; i++) {
arr[i].custom = {};
arr[i].custom["statistics"] = [];
for (let x = 0; x < arr[i].statistic.length; x++) {
var s = arr[i].statistic[x].split("(");
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].substr(0, s[1].length - 1)
});
}
}
console.log(arr)