我需要使用 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)”
提前致謝
uj5u.com熱心網友回復:
您正在迭代外部陣列兩次,而不是迭代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?
uj5u.com熱心網友回復:
您的回圈未正確嵌套,并且您的第二個回圈未迭代正確的專案。您每次都在重新創建 [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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/407905.html
標籤:
