Javascript loop through object array?

javascript loop through array of objects es6
javascript iterate object key, value
loop through object javascript es6
loop through two arrays javascript
javascript foreach object
javascript loop through hash
creating multiple objects in a loop javascript
how to iterate over array of objects in js

I am trying to loop through the following:

{
    "messages": [{
        "msgFrom": "13223821242",
        "msgBody": "Hi there"
    }, {
        "msgFrom": "Bill",
        "msgBody": "Hello!"
    }]
}

I want to retrieve msgFrom and msgBody

I've tried:

        for (var key in data) {
           var obj = data[key];
           for (var prop in obj) {
              if(obj.hasOwnProperty(prop)){
                console.log(prop + " = " + obj[prop]);
              }
           }
        }

But the console log prints [Object]

Any ideas what im doing wrong?

It appears you may just have missed the "messages" property in the data, so the loop is likely iterating the root Object rather than the Array:

for (var key in data.messages) {
    var obj = data.messages[key];
    // ...
}

Unless data was set to messages before the given snippet.

Though, you should consider changing that to a normal for loop for the Array:

for (var i = 0, l = data.messages.length; i < l; i++) {
    var obj = data.messages[i];
    // ...
}

Looping through objects in JavaScript, Then, you loop through the array. You can convert an object into an array with three methods: Object.keys; Object.values; Object. Once in a while, you may need to loop through Objects in JavaScript. The only way to do so before ES6 is with a `forin` loop. The problem with a `forin` loop is that it iterates through properties in the Prototype chain. When you loop through an object with the `forin` loop, you need to

In your script, data is your whole object.

key is "messages", which is an array you need to iterate through like this:

    for (var key in data) {
       var arr = data[key];
       for( var i = 0; i < arr.length; i++ ) {
           var obj = arr[ i ];
           for (var prop in obj) {
               if(obj.hasOwnProperty(prop)){
                   console.log(prop + " = " + obj[prop]);
               }
           }
       }
    }

Looping JavaScript Arrays Using for, forEach & More ‍ , And don't forget a JavaScript object is just a special array and you The following is an example of using JavaScript to loop through an array. To loop through object properties in javascript, you can use the for…in loop and Object.keys() plus forEach. The for…in loop will loop through all the object keys including those inherited from the prototype, so you need to use hasOwnProperty to make sure you are only working on the keys belong to the object itself.

You can use forEach method to iterate over array of objects.

data.messages.forEach(function(message){
    console.log(message)
});

Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

forin, A forin loop only iterates over enumerable, non-Symbol properties. Objects created from built–in constructors like Array and Object have  In this tutorial, we are going to learn different ways to loop through an array of objects in JavaScript. First way: ForEach method. In es6 we have a forEach method which helps us to iterate over the array of objects.

All the answers provided here uses normal function but these days most of our code uses arrow functions in ES6. I hope my answer will help readers on how to use arrow function when we do iteration over array of objects

let data = {
      "messages": [{
           "msgFrom": "13223821242",
           "msgBody": "Hi there"
       }, {
          "msgFrom": "Bill",
          "msgBody": "Hello!"
       }]
 }

Do .forEach on array using arrow function

 data.messages.forEach((obj, i) => {
     console.log("msgFrom", obj.msgFrom);
     console.log("msgBody", obj.msgBody);
 });

Do .map on array using arrow function

 data.messages.map((obj, i) => {
     console.log("msgFrom", obj.msgFrom);
     console.log("msgBody", obj.msgBody);
 });

javascript loop through array and object properties, javascript loop through array and object properties. To loop through an array in javascript, you can use for loop which the syntax is almost the same as in other languages such as java, c++, php, etc. There is also the forEach function that comes with array objects. JavaScript Array Loops There are different ways to loop over arrays in JavaScript, but it can be difficult choosing the right one. Plus keeping each method straight can drive a developer nuts. There is a classic JavaScript for loop, JavaScript forEach method and a collection of libraries with forEach and each helper methods.

The suggested for loop is quite fine but you have to check the properties with hasOwnProperty. I'd rather suggest using Object.keys() that only returns 'own properties' of the object (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)

var data = {
    "messages": [{
        "msgFrom": "13223821242",
        "msgBody": "Hi there"
    }, {
        "msgFrom": "Bill",
        "msgBody": "Hello!"
    }]
};

data.messages.forEach(function(message, index) {
    console.log('message index '+ index);
    Object.keys(message).forEach(function(prop) {    
        console.log(prop + " = " + message[prop]);
    });
});

Iterating over arrays and objects in JavaScript, for loops,; array methods (courtesy of ECMAScript 5 [1]),; listing property keys. It concludes with best practices for applying these approaches. for  I have a JavaScript object like the following: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; Now I want to loop through all p elements (p1, p2, p3

Ways to Loop Through Objects in JavaScript, values() and Object.entries(). The main idea is the convert the object into an array and use array looping methods to loop through that array. Let's begin with the  In this tutorial, we will review how to create an object, what object properties and methods are, and how to access, add, delete, modify, and loop through object properties. Creating an Object An object is a JavaScript data type , just as a number or a string is also a data type.

Looping through an array of objects, Learn Javascript Programming #4: For Loops (Looping Through Numbers, Arrays​, Objects Duration: 3:36 Posted: Dec 18, 2015 JavaScript supports different kinds of loops: for - loops through a block of code a number of times. for/in - loops through the properties of an object. for/of - loops through the values of an iterable object. while - loops through a block of code while a specified condition is true.

How to loop through array of objects in JavaScript(es6), In this tutorial, we are going to learn different ways to loop through an array of objects in JavaScript. First way: ForEach method In es… 4 ways loop through items in array in javascript. Photo by Srinivas JD on Unsplash. This is a brief introduction of 4 ways loop through items in array. 1: for. This is the simplest way.

Comments
  • console.log(obj, prop);
  • You'll generally want to try to avoid using for..in for Arrays.
  • The only credible reason not to use for..in with an Array is that the properties may not be returned in the expected order. Otherwise, it's no better or worse than using for..in on any other Object (unexpected properties, properties from the [[Prototype]], etc.).
  • Thanks a ton for helping me change to a regular for loop as well! Accepting as soon as possible.
  • Why should he consider using a normal for loop?
  • @TravisHeeter for..in treats the array as a plain object, iterating all enumerable keys in an order that isn't guaranteed, rather than just its indices, 0 to length - 1. At times, that may actually be desired. Typically not. – Why is using "for...in" with array iteration a bad idea?
  • @JonathanLonowski Thanks, maybe you can add that to your answer?
  • this one is much simple and efficient
  • Thank you Tarvo, your suggestion worked perfectly for my needs.
  • While your code may provide the answer to the question, please add context/explanation to it so others will have some idea what it does and why it is there.