-1

I need to count the number of online user in this nested object, and return the total.

I've tried this code but I am kind of lost

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) {
    let c = 0;
    for (let i in obj) {
        for (let j in i) {
            if (j.online === true) {
                c++;
            }
        }
    }
    return c;

}

it returns 0

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
Redcoat
  • 25
  • 7

3 Answers3

1

It would probably be better to use reduce - iterate over the Object.values of the object, and add the .online value to the accumulator:

const users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};
const usersOnline = Object.values(users).reduce((a, { online }) => a + online, 0);
console.log(usersOnline);

To fix your original code, you only need to loop once, over every property of users, not only every property of users and over every nested property as well (you only need the online property). You also need to access obj[i], because i is a property, not a value:

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

  let c = 0;
  for (let i in obj) {
    if (obj[i].online === true) {
      c++;
    }
  }
  return c;
}
console.log(countOnline(users));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

You just need to do users[i].online in your if condition. No need for nested for.

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

  let c = 0;
  for (let i in obj) {
    if (users[i].online) {
      c++;
    }
  }
  return c;

}

console.log(countOnline(users));

You can also shorten your code and logic as:

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) {
 var c = 0;
 Object.keys(obj).forEach((key) => obj[key].online ? ++c: null);
 return c;
}

console.log(countOnline(users));
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

You could get all the values using Object.values() and filter all the users who are online.

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

const count = Object.values(users).filter(a => a.online).length;

console.log(count)

But, I think you are better of using an array of users instead of having their name as key of an object. If there are more than one user with the same name, this object approach won't work

adiga
  • 34,372
  • 9
  • 61
  • 83