Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to recurse nested fields #22

Open
zhm opened this issue Sep 13, 2016 · 0 comments
Open

Add function to recurse nested fields #22

zhm opened this issue Sep 13, 2016 · 0 comments

Comments

@zhm
Copy link
Member

zhm commented Sep 13, 2016

Add a function to handle the common pattern of wanting "all elements under this container" (section or repeatable).

Currently we have:

// all direct child element of the repeatable
FIELD('repeatable').elements

But many, if not most times, it's desirable to have all of the repeatable's descendants.

FIELDS('repeatable');

Possible implementation:

function FIELDS(dataName) {
  var eachRecursive = function(elements, iterator) {
    elements.forEach(function(element) {
      if (element.elements) {
        eachRecursive(element.elements, iterator);
      }

      iterator(element);
    });
  };

  var elements = [];

  eachRecursive(FIELD(dataName), function(element) {
    elements.push(element);
  });

  return elements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant