从嵌套对象和数组中提取属性
这个问题可能在过去已经得到了回答,但我似乎在迭代嵌套结构时遇到了麻烦,并且找不到任何可以指引我正确方向的东西。下面是我试图解析的对象。
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 和递归函数等,但从未获得我需要的值或错误,因为该方法无法应用于对象。我希望得到一些有关如何实现这一目标的解释。
This one may have been answered in the past, but I seem to be having trouble iterating through a nested structure and cannot find anything that will point me in the right direction. Below is the object I am trying to parse.
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 }
] } ] }
I want to iterate nestedArray
and from topics
extract the the topic name i.e."topic1" and the key (name) in the associated key-value properties in the nested array i.e. "id" and "error-code".
I am new to this and have tried Object.entries, Object.keys, Object.values and a recursive function etc but never get the values I need or error because the method cant be applied to the Object. I would appreciate some explanation regarding how this is achieved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似的东西?
Something like that ?