Filtering another way

Filtering another way
0

#1

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


#2

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);

#3

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);

#4

FYI - The above assume there is a property named false, but since online is not an object, then it would not have a false property. This would cause user.online.false to return the value undefined.


#5

Right. Dot notation. Brain fart there