我有一個帶有結構的嵌套字典{ "1" : [{"a" : ["p", "q"]}, "b", "c", {"d" : ["h", "w"]}] }。這是
import React from "react";
class TagTree extends React.Component {
render() {
return (
<React.Fragment>
<ul>
{Array.isArray(this.props.tag) ? (
this.props.tag.map((t, index) => {
return (
<TagTree
tag={t}
key={this.props.path index}
path={this.props.path index}
/>
);
})
) : typeof this.props.tag === "object" ? (
Object.entries(this.props.tag).map(([key, t], index) => (
<>
<TagTree
key={this.props.path key}
tag={key}
path={this.props.path key}
/>
<TagTree
key={this.props.path key index}
tag={t}
path={this.props.path key index}
/>
</>
))
) : (
<li>{this.props.tag}</li>
)}
</ul>
</React.Fragment>
);
}
}
class TagApp extends React.Component {
render() {
return (
<div>
<TagTree tag={this.props.tagList} path="" />
</div>
);
}
}
export default TagApp;
uj5u.com熱心網友回復:
我建議使用具有固定鍵的物件串列。例如,每個物件可能看起來像這樣
courses = [
{
"name": "Algebra",
"topics": [
{
"name": "Matrices",
"topics": [
// ...
]
},
// ...
]
},
// ...
]
現在遞回下降到 this 要容易得多,因為每個物件總是有一個"name"and "topics"。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/520631.html
標籤:反应字典递归
上一篇:對字串的偶數值求和
