How to iterate over key/values of an object in Javascript

The standard way to get all the key/values of an object are to iterate over the objects's keys using Object.keys() (opens new window). Once you have all they keys of an object, you can simply get the corresponding values and do whatever you like with them.

Copy code
      
    


One thing to note is that Object.keys() does not guarantee an order of the keys so in the example above, you might get propB before propA. To always get the keys in the same order, you can perform a sort() on the keys and that will guarantee the order of the keys.

Join our community

Your sign-up could not be saved. Please try again.
Your sign-up was successful.

Sign-up and get tips to becoming a software engineer.