What is the difference between ( for… in ) and ( for… of ) statements in JavaScript?

前端 未结 14 1479
慢半拍i
慢半拍i 2020-11-22 06:22

I know what is for... in loop (it iterates over key), but heard the first time about for... of (it iterates over value).

I am confused with

14条回答
  •  心在旅途
    2020-11-22 06:25

    When I first started out learning the for in and of loop, I was confused with my output too, but with a couple of research and understanding you can think of the individual loop like the following : The

    1. for...in loop returns the indexes of the individual property and has no effect of impact on the property's value, it loops and returns information on the property and not the value. E.g

    let profile = { name : "Naphtali", age : 24, favCar : "Mustang", favDrink : "Baileys" }

    The above code is just creating an object called profile, we'll use it for both our examples, so, don't be confused when you see the profile object on an example, just know it was created.

    So now let us use the for...in loop below

    for(let myIndex in profile){
        console.log(`The index of my object property is ${myIndex}`)
    }
     // Outputs : 
            The index of my object property is 0
            The index of my object property is 1
            The index of my object property is 2
            The index of my object property is 3
    

    Now Reason for the output being that we have Four(4) properties in our profile object and indexing as we all know starts from 0...n, so, we get the index of properties 0,1,2,3 since we are working with the for..in loop.

    1. for...of loop* can return either the property, value or both, Let's take a look at how. In javaScript, we can't loop through objects normally as we would on arrays, so, there are a few elements we can use to access either of our choices from an object.

      • Object.keys(object-name-goes-here) >>> Returns the keys or properties of an object.

      • Object.values(object-name-goes-here) >>> Returns the values of an object.

      • Object.entries(object-name-goes-here) >>> Returns both the keys and values of an object.

    Below are examples of their usage, pay attention to Object.entries() :

    Step One: Convert the object to get either its key, value, or both.
    Step Two: loop through.
    
    
    // Getting the keys/property
    
       Step One: let myKeys = ***Object.keys(profile)***
       Step Two: for(let keys of myKeys){
                 console.log(`The key of my object property is ${keys}`)
               }
    
    // Getting the values of the property
    
        Step One: let myValues = ***Object.values(profile)***
        Step Two : for(let values of myValues){
                     console.log(`The value of my object property is ${values}`)
                   }
    

    When using Object.entries() have it that you are calling two entries on the object, i.e the keys and values. You can call both by either of the entry. Example Below.

    Step One: Convert the object to entries, using ***Object.entries(object-name)***
    Step Two: **Destructure** the ***entries object which carries the keys and values*** 
    like so **[keys, values]**, by so doing, you have access to either or both content.
    
    
        // Getting the keys/property
    
           Step One: let myKeysEntry = ***Object.entries(profile)***
           Step Two: for(let [keys, values] of myKeysEntry){
                     console.log(`The key of my object property is ${keys}`)
                   }
    
        // Getting the values of the property
    
            Step One: let myValuesEntry = ***Object.entries(profile)***
            Step Two : for(let [keys, values] of myValuesEntry){
                         console.log(`The value of my object property is ${values}`)
                       }
    
        // Getting both keys and values
    
            Step One: let myBothEntry = ***Object.entries(profile)***
            Step Two : for(let [keys, values] of myBothEntry){
                         console.log(`The keys of my object is ${keys} and its value 
    is ${values}`)
                       }
    

    Make comments on unclear parts section(s).

提交回复
热议问题