简体   繁体   中英

How do I read properties in a nested object and return number if items that meet a condition?

I have the code below, which looks fine to me but keeps returning 0. I need to iterate through the named users in the user object and return the total number of users that have an online property value of true.

I've tried changing the newUser variable to an array and pushing the user into that array of online has a value of true, then returning the length of the array and it returns 0 as well.


    let users = {
      Alan: {
        age: 27,
        online: false
      },
      Jeff: {
        age: 32,
        online: true
      },
      Sarah: {
        age: 48,
        online: false
      },
      Ryan: {
        age: 19,
        online: true
      }
    };

    function countOnline(obj) {
      // change code below this line
      const newUser = 0;

      for (let user in obj) {
        if (user.online === true) {
        } else {
          newUser++;
        }
      }

      return newUser;
      // change code above this line
    }

    console.log(countOnline(users));

I'm expecting the for-in loop to iterate through the users in the user object and if there is a property of online with a value of true to increment the newUser varibale by 1 each time and then return the newUser variable with a value of 2. But it keeps returning 0.

You can use Array.reduce to reduce the array of users to a number

const count = Object.keys(users).reduce((accum, username) => users[username].online ? accum + 1 : accum, 0)

or Array.filter to get the length of a filtered array of online users

const count = Object.keys(users).filter(username => users[username].online).length;

o(n) solution without converting the object keys to an array

let count = 0
for (const username in users) {
   count = users[username].online ? count + 1 : count
}
return count

You can do this alternately with Array.filter ;

 let users = { Alan: { age: 27, online: false }, Jeff: { age: 32, online: true }, Sarah: { age: 48, online: false }, Ryan: { age: 19, online: true } }; let new_user_count = Object.keys(users).filter(x => users[x].online).length; console.log(new_user_count); 

you are iterating an object, and expect it to behave like an array with you for.

given the fact that you are iterating an object, you code should look like this:

  let users = { Alan: { age: 27, online: false }, Jeff: { age: 32, online: true }, Sarah: { age: 48, online: false }, Ryan: { age: 19, online: true } }; function countOnline(obj) { // change code below this line let newUser = 0; for (let user of Object.values(obj)) { if (user.online === true) { newUser++; } } return newUser; // change code above this line } console.log(countOnline(users)); 

also keep in mind that you declared newUser as a const while you are changing it, so the correct way to do it is to declare it with let.

Use const instead of let. Mantain the function countUsersOnline .

const users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

const countUsersOnline = (users) => Object.keys(users).filter(x => users[x].online).length;

console.log(countUsersOnline(users));
countOnline(users)
{
   var a = 0;
   for(let user in users){ 
     if(users[user].online) 
       ++a; 
   }
   return a;
}

The function will check for each object within the object for online property if it is true then it will increment the count flag for online users

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM