# Basic Data Structure

**URL:** <https://forum.freecodecamp.org/t/basic-data-structure/391956>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 21, 2020, 4:50am UTC](https://forum.freecodecamp.org/t/basic-data-structure/391956 "2020-05-21T04:50:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nameLorenzo](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@nameLorenzo](https://forum.freecodecamp.org/u/nameLorenzo)\
**Post date:** [May 21, 2020, 4:50am UTC](https://forum.freecodecamp.org/t/basic-data-structure/391956/1 "2020-05-21T04:50:15Z")

</div>

**Tell us what’s happening:**  
\*\*Can anyone tell me whats going on here in code \*\*  
_Please explain what is in the for loop and rhe if sttement_

**Thank you!!!**

**Your code so far**

```js

freeCodeCamp Challenge Guide: Iterate Through the Keys of an Object with a for…in Statement
Guide

camperbot
1 
Oct '19
Iterate Through the Keys of an Object with a for…in Statement 192
Problem Explanation
Note: dot-notation will cause errors in this challenge.
[square-bracket] notation must be used to call a variable property name.
The following code will not work.
Hints
Hint 1:
for (let user in obj) {
  if (obj.user.online === true) {
    //code
  }
}
Hint 2
Example 2 demonstrates how using [square-bracket] notation the code will be executed.
for (let user in obj) {
  if (obj[user].online === true) {
    //code
  }
}
Solutions
Solution 1 (Click to Show/Hide)
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 result = 0;
  for (let user in obj) {
    if (obj[user].online === true) {
      result++;
    }
  }
  return result;
  // change code above this line
}
console.log(countOnline(users));

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Linux; Android 8.1.0; CPH1909) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Mobile Safari/537.36`.

**Challenge:** Iterate Through the Keys of an Object with a for…in Statement

**Link to the challenge:**  
[https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement)

---

<div class="post-metadata">

**Author:** ![imendieta](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/imendieta/32/71340_2.png) [@imendieta](https://forum.freecodecamp.org/u/imendieta)\
**Post date:** [May 21, 2020, 6:58am UTC](https://forum.freecodecamp.org/t/basic-data-structure/391956/2 "2020-05-21T06:58:20Z")

</div>

Hi @nameLorenzo

First we need to understand how our object is composed.  
You have this object called “users” right ? you know that inside “users” we have data  
“user” meaning user will be “Alan”, “Jeff”, “Sarah” those will call them  
for now user the names.  
Each user have a nested object with again a key value pair of “Online”  
with a key pair of value either true (1) or false (0).

```auto
var users = {
  Alan: {
    online: false
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

```

In order to get true or false we need to set our create a flag variable a  
true or false. This can be  
`var result = 0;` to start our flag.  
Now lets loop or iterate the “users object”.  
let user be the current property names the names because we want to iterate or loop all of them. so we do for in loop. (all of the properties(user) of the object(users))

```auto
 for (let user in users) {

   }

```

Once we have this. we can do an if else statement to check its key value pair if either  
true (1) or false (0).

` let result = 0;`  
we access the “users” object and access all of its current properties[user] and access it’s key values online and set it to true to find out the key value if its true or false.

` users[user].online === true`  
and we add 1 to it meaning its true;  
`result++;`  
else will be 0 as default in the return.  
` return result;`

---

<div class="post-metadata">

**Author:** ![nameLorenzo](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@nameLorenzo](https://forum.freecodecamp.org/u/nameLorenzo)\
**Post date:** [May 21, 2020, 7:27am UTC](https://forum.freecodecamp.org/t/basic-data-structure/391956/3 "2020-05-21T07:27:33Z")

</div>

Thanks man!!  
You actually explain what im looking for, now I can understand what is doing of the if statement  
**Thank you again!!!**

---

<div class="post-metadata">

**Author:** ![imendieta](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/imendieta/32/71340_2.png) [@imendieta](https://forum.freecodecamp.org/u/imendieta)\
**Post date:** [May 21, 2020, 7:34am UTC](https://forum.freecodecamp.org/t/basic-data-structure/391956/4 "2020-05-21T07:34:23Z")

</div>

Glad to help out 🙂

Here is a link for in loop take a look at it will help you understand better the types of loops and Looping through objects from ES5 and ES6 syntax loops.

> **[Looping through objects in JavaScript | Zell Liew](https://zellwk.com/blog/looping-through-js-objects/)**
>
> Once in a while, you may need to loop through Objects in JavaScript. The only way to do so before ES6 is with a \`for...in\` loop. The problem with a \`for...in\` loop is that it iterates through properties in the Prototype chain. When you loop through...

happy Coding…

---

<div class="post-metadata">

**Author:** ![nameLorenzo](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@nameLorenzo](https://forum.freecodecamp.org/u/nameLorenzo)\
**Post date:** [May 21, 2020, 7:55am UTC](https://forum.freecodecamp.org/t/basic-data-structure/391956/5 "2020-05-21T07:55:16Z")

</div>

Ow okay i’ll read it 🤩 🤩

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 12:53pm UTC](https://forum.freecodecamp.org/t/basic-data-structure/391956/6 "2021-06-01T12:53:42Z")

</div>

Closed on June 1, 2021, 12:53pm UTC


