Answer the question
In order to leave comments, you need to log in
What is the best way to work with data array?
Help, because I have a crisis. I can’t catch up with how to implement this:
From the server, from MongoDb, an array of objects of the following form comes:
let data = [
{
name: 'string',
val1: int,
val2: int,
nodes: [
{
name: 'string',
val1: int,
val2: int,
nodes: [ ... ]
},
{
// ...
}
]
},
{
// ...
}
]
_'${name} | ${val1} | ${val2= val1+childs val2}'
|___ '${name} | ${val1} | ${val2=val1+childs val2}' // child
|_____ .....
Answer the question
In order to leave comments, you need to log in
This is a normal recursive tree traversal. Adding\removing\editing will depend on how (and where) you store the data - flux\redux\whatever. In the case of redux, I would store the tree in state in a flat form (you will need id, but I think that with mongo this is not a problem).
You can display the tree like this:
const MySuperTree => (props) => (
<ul>{props.nodes.map(node => <TreeNode {...node} />)}</ul>
)
class TreeNode extends PureComponent {
render() {
const { name, val1, val2, nodes } = this.props
return (
<li>
<b>{name} | {val1} | {val1+getValFromChild(childs)} {val2}</b>
{nodes && nodes.length > 0 && (
<MySuperTree nodes={nodes} />
)}
</li>
)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question