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

New method: _.deepKeys #45

Open
zaggino opened this issue May 8, 2015 · 0 comments
Open

New method: _.deepKeys #45

zaggino opened this issue May 8, 2015 · 0 comments

Comments

@zaggino
Copy link

zaggino commented May 8, 2015

There're certainly situations where I could use this method:

var src = {
  "a": true,
  "b": {
    "ba": true,
    "bb": true
  }
}
var arr = _.deepKeys(src);
// returns ["a", "b.ba", "b.bb"]

This is my quick implementation which I needed:

function deepKeys(obj, path) {
  path = path || [];
  if (_.isArray(obj)) {
    path.push('0');
    obj = obj[0];
  }  
  return _.reduce(obj, function (result, value, key) {
    if (_.isPlainObject(value) || _.isArray(value)) {
      return result.concat(deepKeys(value, path.concat(key)));
    }
    result.push(path.concat(key).join("."));
    return result;
  }, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant