Q:

Returning a new set of array using map method

How do I return a new set of array using map based on 2 conditions:

The array length should not be 0
role should be moderator

This is my original response looks like

React function which maps the response
_getList(memberships) {
    const items = memberships.map((membership) => {
        return (
            <RadioButton
                value={membership.forum.slug}
                label={membership.forum.title} />
        );
    });

    return(
        <RadioButtonGroup>
            {items}
        </RadioButtonGroup>
    );
}

In my render function I am checking if the length is not 0
const renderedList = this._getList(UserStore.getState().memberships.length !== 0);

How should I add another check that the new array returned should only contains the data with role === moderator

A:

Using filter
_getList(memberships) {
    const items = (memberships.filter(membership => membership.role === 'moderator') || []).map((membership) => {
        return (
            <RadioButton
                value={membership.forum.slug}
                label={membership.forum.title} />
        );
    });

    if(items.length === 0) return null;

    return(
        <RadioButtonGroup>
            {items}
        </RadioButtonGroup>
    );
}

then you could do this:
const renderedList = this._getList(UserStore.getState().memberships);

if(renderedList) {
    this.renderSomething();
} else {
    this.renderSomethingElse();
}

