Skip to content

Recursion #2

Description

@at0g

Hello, would you be willing to support recursion on objects/arrays?

I've implemented it in my project using the following (ES6) - I would have sent you a PR, but coffee script is not my thing.

// Apply a morph function recursively
function morphObjectProps (obj, fn) {
    if (!obj) {
        return obj;
    }

    if (Array.isArray(obj)) {
        return obj.map((item) => {
            return morphObjectProps(item, fn);
        });
    }

    return Object.keys(obj).reduce((memo, key) => {
        let value = obj[key];

        if (typeof val === 'object') {
            memo[fn(key)] = morphObjectProps(value);
        }
        else {
            memo[fn(key)] = value;
        }
        return memo;
    }, {});
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions