WBxs
Feb 14, 2018

Welcome to our tutorial, now I am going to explain how to loop through javascript array of objects with examples.

protect your javascript code

By the end of this tutorial you will be aware of:

What are Javascript Loops?

Javascript loops allow you to repeat code blocks as specified by user. You can find more details about loop types in previous article "Best Way To Loop Through Javascript Array".

How To Loop Through Javascript Array Of Objects?

There are mainly two ways to loop through object as follow:
var person = {firstName:"Adam", lastName:"Hassan"};
var keys = [];
for(var k in person){
keys.push(k);
document.write(person[k]);
document.write("<BR>");
}
document.write("total " + keys.length + " keys: " + keys);
Or
var person = { first: "Adam", last: "Hassan" };
Object.keys(person).forEach(function(key) {
document.write(key, person[key]);
});

Javascript Array Of Objects Loop Example

Next example shows how to use objects loop:
<script>
var person = {firstName:"Adam", lastName:"Hassan"};
var keys = [];
for(var key in person){
//use array to store properties of an object

keys.push(key);
//also you can make sure that the key you get is an actual property of an object

if (person.hasOwnProperty(key)) {
document.write(key + " -> " + person[key]);
document.write("<BR>");
}
}
document.write("total " + keys.length + " keys: " + keys);
</script>

Try WhiteBoxes JavaScript Obfuscator >>

Thank you for reading the above article. Please let us know your comments below.

References:
  • Practical Experience
About the author

Adam Hassan

Adam is a freelance web designer, recreational software developer, author and CEO of infoapper.com . Also, has passion to learn and transfer the know-how of every valuable thing.
Views: 249
× Close
Results:
Want to Enable Key Features? Create a Free Account.