B
B
bogushvitaly2014-05-30 17:38:39
JavaScript
bogushvitaly, 2014-05-30 17:38:39

How to traverse a tree recursively?

How to recursively get width value from all nesting levels?
jsbin.com/gecabacu/1/edit?js

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-06-20
@bogushvitaly

const getWidths = data =>
  [].concat(data || []).reduce((acc, n) => {
    if (n.hasOwnProperty('width')) {
      acc.push(n.width);
    }
    acc.push(...getWidths(n.columns));
    return acc;
  }, []);

https://jsfiddle.net/jf0hsm6c/

I
Ilya Lesnykh, 2014-05-30
@Aliance

for in + recursion. Or is it as difficult as using the "code" button to properly format the question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question