Mohamed Sobhy
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

Mohamed Sobhy

Mohamed 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: 1060
0 Comment Guest
Recommend 0
Sort by Newest
Be the first to say something...

Ask Community

Ask questions and Share knowledge with Community

Find below recent posts for automation solutions with questions and answers by community. You can search in past threads or post new question about your assignment with detailed description, and always could mark your question as request. Sharing knowledge are highly appreciated by answering on others questions, and in return awards will be decided.

× Close
Results: