score:1
Accepted answer
i'm guessing the problem is simply that you don't declare branches
inside your function, making it a global variable. so the next time the function is run, you're appending to it. adding a const
declaration should probably do.
that said, i think you can simplify getbranches
in a clean manner:
const getbranches = (o) => object .entries (o) .flatmap (
([key, {label, type, nodes}]) => [
... (type == types [0] ? [] : [{key, label}]),
... (nodes ? getbranches (nodes) : [])
]
)
const types = [0, 1]
const treedata = {"first-level-node-1": {label: "regija", type: types[1], index: 0, url: "http: //opa.com", nodes: {"second-level-node-1": {label: "bolnica bn", index: 0, type: 1, nodes: {"third-level-node-1": {label: "konektor sysmex", index: 0, nodes: {}, type: types[0]}}}}}, "first-level-node-2": {label: "regija 2", index: 1, type: types[1], nodes: {"2-1": {label: "dz trebinje", index: 0, type: 1, nodes: {"3-1": {label: "konektor biomerux", index: 0, hasnodes: false, type: types[0]}}}}}}
console .log (getbranches (treedata))
.as-console-wrapper {max-height: 100% !important; top: 0}
Source: stackoverflow.com
Related Query
- Recursive function and React in Crome debug VM - a mess
- react link vs a tag and arrow function
- How Snapshot testing works and what does toMatchSnapshot( ) function do in Jest Snapshot testing for React components?
- React debug Using VSCode and Firefox instead of Chrome
- How to fix nextCreate is not a function setting up useMemo setting up authentication react router and hooks
- What's the difference between a JavaScript function and a React hook?
- React memo components and re-render when passing function as props
- Test if function is called react and enzyme
- how to access vairables outside of map function in js and jsx in a React component
- React JS : history.push is not a function error and it isn't navigating to a different page onclick of swal
- How to run and debug a react app directly from VSCode?
- Mock React useRef or a function inside a functional component with enzyme and jest?
- React Bootstrap and React.PropTypes validation function
- TypeError: Object(...) is not a function with React Table and moment.js
- Mock a non-default function using Jest and React
- Testing debounced function in React component with Jest and Enzyme
- stop setInterval and if not work in react native function
- _this.store.getState is not a function when testing react component with enzyme and mocha
- React 16: Call children's function from parent when using hooks and functional component
- Obscurify react code in production by automatically changing all the function and variable names
- Sync queryParameters with Redux state and react router for function components
- React and Redux: Dispatching all actions within one function
- setTimeout() function is not detecting a state change and keeps executing the recursive function
- In React why does calling a function from another function in our code not work and display?
- How to export function and import react hooks
- react js how to import only the required function from a file and not the all functions
- How to Separate Jsx inside render function into a separate file in ES6? or any other solution in react to separate the Logic and presentation?
- React refs when parent and child are function components
- How to test with jest and typescript with types a basic react function component
- React input text default value and onblur function make input text read only
More Query from same tag
- How do I link to local pdf file in a create-react-app project?
- Creating component for forms in react
- Rabbit-MQ with React
- How do I mock only one value returned by a custom hook?
- Reactjs with JSX: Unexpected token '<'
- Angular 1.X ng-options equivalent using React
- setState callback not working as expected
- React: setState of array of object changes every item
- Gatsby wildcard route
- Create initialState on Store with Redux
- React set className by conditions dynamic
- TypeError: comments.map is not a function ReactJs
- How do I access a property within an array for my function?
- Data from my react front end not getting to mysql database
- Reactstrap DropdownMenu bottom overflow issue
- How to refer to conditional elements when unit testing React components using jest and enzyme
- ReactJS: How to fix TypeError: data is not iterable
- When to use componentWillReceiveProps lifecycle method?
- How do I use localstorage.getItem or setItem to send a post request in my react application?
- I have an addEvent function but the correct state is not being passed to it
- I'm using React-Bootstrap. The e.preventDefault() is not working
- Redux not updating state correctly -- state stays as empty array despite adding an object in the line prior
- How can I keep the data after hiding it?
- How do I create polar charts with highcharts-react-official?
- Setting setTimeOut in a react class base component
- Call componentDidMount when API responds
- React pass params to the node elements from child component
- React app with wordrpess on nginx won't work on HTTPS
- BrowserRouter not working in create-nw-react-app
- No matching files ... were found. (ReactJS)