我需要使用 Javascript 將這個 file.dat 轉換為一個陣列以進行練習。
Team P W L D F A Pts
1. Arsenal 38 26 9 3 79 - 36 87
2. Liverpool 38 24 8 6 67 - 30 80
3. Manchester_U 38 24 5 9 87 - 45 77
4. Newcastle 38 21 8 9 74 - 52 71
5. Leeds 38 18 12 8 53 - 37 66
6. Chelsea 38 17 13 8 66 - 38 64
7. West_Ham 38 15 8 15 48 - 57 53
8. Aston_Villa 38 12 14 12 46 - 47 50
9. Tottenham 38 14 8 16 49 - 53 50
10. Blackburn 38 12 10 16 55 - 51 46
11. Southampton 38 12 9 17 46 - 54 45
12. Middlesbrough 38 12 9 17 35 - 47 45
13. Fulham 38 10 14 14 36 - 44 44
14. Charlton 38 10 14 14 38 - 49 44
15. Everton 38 11 10 17 45 - 57 43
16. Bolton 38 9 13 16 44 - 62 40
17. Sunderland 38 10 10 18 29 - 51 40
-------------------------------------------------------
18. Ipswich 38 9 9 20 41 - 64 36
19. Derby 38 8 6 24 33 - 63 30
20. Leicester 38 5 13 20 30 - 64 28
在網上沖浪時,我找到了決議檔案和創建陣列的代碼。我作業但現在但我遇到了一些問題:因為在最終陣列中我有很多不需要的字符作為空格,團隊名稱之前的數字和僅由 ----- 形成的行
這是 Html 代碼和我的 script.js
function fileToArray(str, delimiter = /[#\s,] /g) {
// slice from start of text to the first \n index
// use split to create an array from string by delimiter
const headers = str.slice(0, str.indexOf("\n")).split(delimiter);
console.log(headers);
const rows = str.slice(str.indexOf("\n") 1).split(delimiter);
console.log(rows);
// Map the rows
// split values from each row into an array
// use headers.reduce to create an object
// object properties derived from headers:values
// the object passed as an element of the array
const arr = rows.map(function(row) {
const values = row.split(delimiter);
const el = headers.reduce(function(object, header, index) {
object[header] = values[index];
return object;
}, {});
return el;
});
// return the array
return arr;
console.log(arr);
}
const data = fileToArray(text);
var Json = JSON.stringify(data);
console.log(Json);
使用元字符 /[#\s,] /g 我能夠消除一些空格,但現在我不知道如何改進代碼。
編輯:結果必須是這樣的 JSON:
{
"0": {
"Team":"Arsenal",
"P": 38,
"W":26,
"L":9,
"D":3,
"F":79,
"A":36,
"Pts": 87
},
"1": {
"Team":"Liverpool",
"P": 38,
"W":24,
"L":8,
"D":6,
"F":67,
"A":30,
"Pts": 80
},
...
}
uj5u.com熱心網友回復:
也許像這樣
我在修剪后破壞線條
如果您想保留數字并使用物件而不是陣列
function fileToArray(text) {
const lines = text.split(/\n/); // split on \n
const header = lines[0].trim().split(/\s /); // split on whitespace after trimming
return [...lines.slice(1).map(line => {
// ignore the number and the dash
const [number, team, p, w, l, d, f, ignore2, a, pts] = line.trim().split(/\s /);
return {[number]: {team, p, w, l, d, f, a, pts}}
})]
}
console.log(
fileToArray(text)
)
<script>
const text = ` Team P W L D F A Pts
1. Arsenal 38 26 9 3 79 - 36 87
2. Liverpool 38 24 8 6 67 - 30 80
3. Manchester_U 38 24 5 9 87 - 45 77
4. Newcastle 38 21 8 9 74 - 52 71
5. Leeds 38 18 12 8 53 - 37 66
6. Chelsea 38 17 13 8 66 - 38 64
7. West_Ham 38 15 8 15 48 - 57 53
8. Aston_Villa 38 12 14 12 46 - 47 50
9. Tottenham 38 14 8 16 49 - 53 50
10. Blackburn 38 12 10 16 55 - 51 46
11. Southampton 38 12 9 17 46 - 54 45
12. Middlesbrough 38 12 9 17 35 - 47 45
13. Fulham 38 10 14 14 36 - 44 44
14. Charlton 38 10 14 14 38 - 49 44
15. Everton 38 11 10 17 45 - 57 43
16. Bolton 38 9 13 16 44 - 62 40
17. Sunderland 38 10 10 18 29 - 51 40
-------------------------------------------------------
18. Ipswich 38 9 9 20 41 - 64 36
19. Derby 38 8 6 24 33 - 63 30
20. Leicester 38 5 13 20 30 - 64 28`
</script>
這是一個陣列版本
function fileToArray(text) {
const lines = text.split(/\n/); // split on \n
const header = lines[0].trim().split(/\s /); // split on whitespace after trimming
return [header, ...lines.slice(1).map(line => {
// ignore the number and the dash
const [ignore1, team, p, w, l, d, f, ignore2, a, pts] = line.trim().split(/\s /);
return [team, p, w, l, d, f, a, pts];
})]
}
console.log(
fileToArray(text)
)
<script>
const text = ` Team P W L D F A Pts
1. Arsenal 38 26 9 3 79 - 36 87
2. Liverpool 38 24 8 6 67 - 30 80
3. Manchester_U 38 24 5 9 87 - 45 77
4. Newcastle 38 21 8 9 74 - 52 71
5. Leeds 38 18 12 8 53 - 37 66
6. Chelsea 38 17 13 8 66 - 38 64
7. West_Ham 38 15 8 15 48 - 57 53
8. Aston_Villa 38 12 14 12 46 - 47 50
9. Tottenham 38 14 8 16 49 - 53 50
10. Blackburn 38 12 10 16 55 - 51 46
11. Southampton 38 12 9 17 46 - 54 45
12. Middlesbrough 38 12 9 17 35 - 47 45
13. Fulham 38 10 14 14 36 - 44 44
14. Charlton 38 10 14 14 38 - 49 44
15. Everton 38 11 10 17 45 - 57 43
16. Bolton 38 9 13 16 44 - 62 40
17. Sunderland 38 10 10 18 29 - 51 40
-------------------------------------------------------
18. Ipswich 38 9 9 20 41 - 64 36
19. Derby 38 8 6 24 33 - 63 30
20. Leicester 38 5 13 20 30 - 64 28`
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/413528.html
標籤:
上一篇:如何從頭開始列印一行?
下一篇:在c 中包含兩個頭檔案
