JSON資料目前看起來像這個字串:
["ID","Name","Age"],["212","David","38"]
而我想讓它看起來像這樣:
{"ID":"212","Name":"David","Age":"38"}提前感謝您的幫助
我找到了這個代碼,它解決了大部分的問題。var columns = ["ID", "Name", "age"] 。
var rows = ["212"/span>, "David"/span>, "38"/span>]。
var result = rows.reduce(function(result, field, index) {
result[columns[index]] = field;
return result。
}, {})
console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以通過以下步驟來實作:
let obj = [["ID"/span>,"Name", "Age"],["212","David", "38"]
let [keys, values] = obj;
let zipped = keys.map((key, i)=>[key, values[i]] )。)
let output = Object.fromEntries(zipped)。
console.log(output)。
uj5u.com熱心網友回復:
我們說 let jsonVal = [["ID", "Name", "Age"], ["212", "David", "38"], ["212", "David", "38"] 第0個索引將有鍵,其余的是資料
。let newJsonVal = [] 。
for (let i =1; i< jsonVal.length-1; i ) {
let newObject ={}。
jsonVal[i].map((d,j) =>; {
newObject[jsonVal[0][j] = d。
})
newJsonVal.push(newObject)
}
newJsonVal將有你需要的物件陣列
。轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/327686.html
標籤:
