我有一個內容類似于此的陣列:
const array = [["name", "age", "city", "email"],["Peter", "30", "Madrid", "[email protected]"],["Josh", "23", "NY", "[email protected]"] ]
我必須以這種格式回傳內容:
<data>
<name>Peter</name>
<age>30</age>
<city>Madrid</city>
<email>[email protected]</email>
<name>Josh</name>
<age>23</age>
<city>NY</city>
<email>[email protected]</email>
</data>
我已經嘗試了所有沒有運氣的想法,所以歡迎任何幫助。
uj5u.com熱心網友回復:
使用for回圈,一個用于迭代陣列,另一個用于迭代值。
const array = [
["name", "age", "city", "email"],
["Peter", "30", "Madrid", "[email protected]"],
["Josh", "23", "NY", "[email protected]"]
]
const labels = array.shift()
let output = '<data>'
for (const item of array) {
for (let i = 0; i < 4; i ) {
output = `<${labels[i]}>${item[i]}</${labels[i]}>`
}
}
output = '</data>'
document.getElementById('txt').value = output
textarea {
width: 100%;
height: 100px;
}
<textarea id="txt"></textarea>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517694.html
上一篇:在移動設定中增加字體大小會使recyclerview專案重疊
下一篇:使用Python進行XML轉換
