簡體   English   中英

遍歷嵌套的 object javascript

[英]Iterate through nested object javascript

我有一個 javascript object 嵌套對象如下:

Object { 
    Id: 1,
    Descendants: [{
      Object {
         id:2
         Descendants:[
           {Object
             {id:3
              Descendants[]
}}]}]

現在我想遍歷所有后代屬性並打印它們。 最干凈的方法是什么? 提前非常感謝!

你可以試試這個:

 const o = { id: 1, Descendants: [ { id: 2, Descendants: [ { id: 3, Descendants: [], }, ], }, ], } function doSomething(o) { // Write Do whatever you want to do with the objects console.log(o.id) // And to the recursively for (const child of o.Descendants) { doSomething(child) } } doSomething(o)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM