1

How can I convert all object properties to strings like they would be paths?

E.g.

{a:{s:"asd",g:"asd"}, b:2}

Output:

["a.s",
 "a.g",
 "b"]

Does exist a function able to do something like this?

1 Answer 1

3

There isn’t one built into Node, but it’s not hard to write recursively:

function descendants(obj) {
    return Object.keys(obj).map(function (key) {
        var value = obj[key];

        // So as to not include 'a'; a bit of a hack.
        // You might need better criteria.
        if (typeof value === 'object') {
            return descendants(value).map(function (desc) {
                return key + '.' + desc;
            });
        }

        return [key];
    }).reduce(function (a, b) {
        return a.concat(b);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.