How do we use 'for' loop with objects?


#1

Hello friends,

I want to know how do I display the contents of particular object using for loops.(whether this is possible at all?)


// Object constructor
var directions = new Object ();
directions.north ="Himalaya";
directions.south = "Indian Ocean"; 
directions.east = "Darjeeling";
directions.west = "Mumbai"
console.log (directions) ;  
// How do I use for loop
for(i =0; i < directions.length ; i ++)  
 console.log ( directions [i] );

Regards
Yugantika


#2

yep, we can use a for in loop:

for (key in directions){
    console.log(key + "goes to: " + directions[key]) 
}

which will give you the keys, which you can use to access the value in the dictionary with bracket notation.


#3

Hi stetim94,

I appreciate your help.
I tried out the below code, but got error i.e (illegal token)

var directions = new Object ();
directions.north ="Himalaya";
directions.south = "Indian Ocean"; 
directions.east = "Darjeeling";
directions.west = "Mumbai"
console.log (directions) ;  
// How do I use for loop
for(i =0; i < directions.length ; i ++){
 console.log(north + "goes to: " + directions[north]) 
};

Could you kindly correct me.

Regards
Yugantiks


#4

Why make it yourself so complicated with the for loop you use? Why not use for .. in?

add this before your loop:

console.log(directions.length)

you will see, the for loop you use is ineffective, use for .. in instead, like i showed


#5

Alright stetim94.
But I am unaware of for.. in loop.


#6

I mean could u show an example of implementation of for... in loop.

As I am new to this coding study,I get many doubts .(which may be often silly!! )
Kindly help.
Regards,
Yugantika


#7

The way objects work allow under the hood won't allow you to use a for loop like you did, you really need for .. in. I already did:


#8

Honestly I don't mean to be a pain and I am very grateful for your help. But I really want to understand rather than copy and paste/adjust.

Here is what I have tried

var directions = new Object ();
directions.north ="Himalaya";
directions.south = "Indian Ocean";
directions.east = "Darjeeling";
directions.west = "Mumbai"

for ( north in directions) {
console.log(north + "goes to: " + directions[key]) }

My intention is to output the 4 values(here places) for 4 keys(directions).
This is not happening after the code correction as provided by you.
May be i am going wrong.

Thanks in advance


#9

well, yea, if you are going to change the code, do it right. Your for loop:

for ( north in directions)

Now north holds the keys:

for ( north in directions){
  console.log(north)

So, now we need to use north the access the values at the keys:

for ( north in directions) {
console.log(north + "goes to: " + directions[north]) }

north holds the keys (since you specify this in your for loop), so we need to use north between the brackets to access the values.


#10

Hi stetim94,

My code gives the output as expected ..
here s the output

{ north: 'Himalaya', south: 'Indian Ocean', east: 'Darjeeling', west: 'Mumbai' }
northgoes to: Himalaya
southgoes to: Indian Ocean
eastgoes to: Darjeeling
westgoes to: Mumbai.

I sincerely appreciate your help. Thanks a lot.
Its because of folk like you I can hereafter start experimenting with code and enrich my knowledge on coding.:slight_smile:

Thanks,
Yugantika:sunglasses:


#11

You figured out this for loop does most of the work for you? :slight_smile:

You are welcome, good to hear you achieved the desired output