从嵌套对象和数组中提取属性

Extracting properties from nested objects and arrays

过去可能有人回答过这个问题,但我似乎在遍历嵌套结构时遇到了问题,而且找不到任何可以指出正确方向的内容。下面是我要解析的对象。

const nestedArray = 
  { id       : 100
  , interval : 1000
  , enable   : true
  , topics: 
    [ { topic1: 
        [ { id: 0, 'error-code' : 100 } 
        , { id: 1, status       : 200 } 
        , { id: 2, mode         : 300 } 
      ] } 
    , { topic2: 
        [ { id: 0, count     : 100 } 
        , { id: 1, total     : 200 } 
        , { id: 2, operation : 300 } 
  ] } ] }

我想迭代 nestedArray 并从 topics 中提取主题名称,即“topic1”和嵌套数组中相关键值属性中的键(名称),即“id " 和 "错误代码"。

我是新手,尝试过 Object.entries、Object.keys、Object.values 和递归函数等,但从未获得我需要的值或错误,因为无法应用该方法到对象。对于如何实现这一点,我将不胜感激。

类似的东西?

const nestedArray = 
  { id       : 100
  , interval : 1000
  , enable   : true
  , topics: 
    [ { topic1: 
        [ { id: 0, 'error-code' : 100 } 
        , { id: 1, status       : 200 } 
        , { id: 2, mode         : 300 } 
      ] } 
    , { topic2: 
        [ { id: 0, count     : 100 } 
        , { id: 1, total     : 200 } 
        , { id: 2, operation : 300 } 
  ] } ] }

nestedArray.topics.forEach( topic =>
  {
  let key0 = Object.keys( topic)[0]
  topic[key0].forEach(el =>
    console.log( key0, '->', Object.keys(el). join(', ')))
  })

console.log( '\notherwise :')

nestedArray.topics.forEach( topic =>
  {
  let key0 = Object.keys( topic)[0]
  topic[key0].forEach(el =>
    console.log( key0, '->', Object.entries(el).map(([k,v])=>`${k}: ${v} `).join(', ')))
  })
.as-console-wrapper {max-height: 100%!important;top:0 }
.as-console-row::after { display:none !important; }