codedamn

Iterating objects

Created by Codedamn about a year ago

0

No description provided

4 Comments

    2

    this one is little tough for beginners & you can include some exercises in it

    @sumitbehal

    Sumit Behal

    @sumitbehal

    1

    Also, it will be great to explain usecase instead of just writing code. It gives a better perspective tbh.

    @sumitbehal

    Sumit Behal

    @sumitbehal

    1

    I didn't liked the explanation. It's quite difficult to understand

    @vishalkr17

    Vishal Kumar

    @vishalkr17

    0

    First defining an object here:

    const userObject = { name: 'Sanjam', class: '+1', crush: true //boolean value }

    The keys are the "name, class and crush" in the object created(userObject), Object.keys creates an array of this and passes it onto variable, here it is passed to keyvalues.

    const keyvalues = Object.keys(userObject) console.log(keyvalues)

    The output of logging: [ 'name', 'class', 'crush' ]

    Similar to keys, the values are Sanjam, +1 and true here.

    const values = Object.values(userObject) console.log(Object.values(userObject))

    The output of logging: [ 'Sanjam', '+1', true ]

    for(let i=0; i<keyvalues.length; i++) { const keystore = keyvalues[i] This gives keystore the current(i) value of the key of the array that keyvalues has recieved from Object.keys. const valuestore = values[i] Similar to keys, here values are recieved. console.log(keystore, "==>", valuestore)

    Other way of logging same output: console.log(`${keystore} ==> ${valuestore}`)
    

    }

    This is a bit I have understood.

    @sanjamkapoor

    S MOVE

    @sanjamkapoor

Your comment