如何在 JSON 中創建類似結構的目錄,插入值存盤在最后一個 JSON 物件中?
輸出示例:
let obj = {
value : 5,
next : {
value : 10,
next : {
value : 15,
next : {
value : 20,
next : null
}
}
}
}
現在,如果我們必須插入 25,那么它必須存盤在值 20 之后的最后一個下一個節點中。
uj5u.com熱心網友回復:
如果obj每個級別只有一個值,那么下面是給你的參考
let obj = {
value : 5,
next : {
value : 10,
next : {
value : 15,
next : {
value : 20,
next : null
}
}
}
}
const insert = (value,data) =>{
let next = data.next
let prev = null
while(next!=null){
prev = next
next = next.next
}
prev.next = {'value':value,next:null}
return data
}
console.log(insert(22,obj))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/533028.html
上一篇:回應式導航漢堡選單不起作用
