Filtering another way

Tell us what’s happening:
So this does not really have to do with the challenge…I just had a thought. In the challenge you have to filter users who are online which can be done by

const usersOnline =this.state.users.filter(user => user.online);

What if I wanted to filter those who are offline?

Your code so far


class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      users: [
        {
          username: 'Jeff',
          online: true
        },
        {
          username: 'Alan',
          online: false
        },
        {
          username: 'Mary',
          online: true
        },
        {
          username: 'Jim',
          online: false
        },
        {
          username: 'Sara',
          online: true
        },
        {
          username: 'Laura',
          online: true
        }
      ]
    }
  }
  render() {
    const usersOnline =this.state.users.filter(user => user.online);
    const renderOnline = usersOnline.map((item)=> <li key={item.username}>{item.username} </li>)
    return (
       <div>
         <h1>Current Online Users:</h1>
         <ul>
           {renderOnline}
         </ul>
       </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/use-array-filter-to-dynamically-filter-an-array

You mean just flip which you want to keep from those who are online to those who are not online?

const usersOnline = this.state.users.filter(user => !user.online);
1 Like

Well I feel dumb lol. I did not think about using !. Here I was trying to do thing like

const usersOnline = this.state.users.filter(user => user.online.false

or

const usersOnline = this.state.users.filter(user => user.online===false);

Right. Dot notation. Brain fart there