antd 根据 json 数据动态生成 menu

如题:根据json数据动态生成 menu,json数据的深度不固定,所以menu不能直接固定死嵌套几层map循环,其中children为submenu,谢谢。

json 数据范例如下:

https://img1.mukewang.com/5c04d76e00018c1b02740894.jpg

根据json范例数据生成的menu如下

https://img1.mukewang.com/5c04d77a0001812d02210361.jpg

哆啦的时光机
浏览 2299回答 1
1回答

陪伴而非守候

用递归可以实现,具体代码如下:class Sider extends Component {&nbsp; constructor() {&nbsp; &nbsp; super()&nbsp; }&nbsp; recursion(dataSource) {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; dataSource.map((menu, index) => {&nbsp; &nbsp; &nbsp; &nbsp; if (menu.children) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SubMenu key={menu.key} title={menu.title}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {this.recursion(menu.children)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </SubMenu>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (<Menu.Item key={menu.key}>{menu.title}</Menu.Item>)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; )&nbsp; }&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <Menu&nbsp; &nbsp; &nbsp; &nbsp; mode="inline"&nbsp; &nbsp; &nbsp; &nbsp; style={{ width: 240 }}&nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.recursion(data)&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; </Menu>&nbsp; &nbsp; );&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript