0

I am trying to learn more about loops and am attempting to access object properties in JSON format.

My JS is:

var movies = {
  "Black Panther" : {
    "title" : "Black Panther",
    "theatricalrelease" : "2/16/2018"
  },
  "Infinity War" : {
    "title" : "Avengers: Infinity War",
    "theatricalrelease" : "5/4/2018"
  },
  "Captain Marvel" : {
    "title" : "Captain Marvel",
    "theatricalrelease" : "TBA"
  }
}

console.log(movies); //logs correctly
console.log(movies.length); //logs undefined

for (var i = 0; i < movies.length; i++) {
  console.log(movies[i]); // doesn't log anything
}

How can I access object properties like title and theatricalrelease?

cfoster5
  • 1,696
  • 5
  • 28
  • 42
  • That isn't an array. – SLaks Feb 01 '18 at 02:13
  • Possible duplicated question of: https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object – caiovisk Feb 01 '18 at 02:15
  • 2
    Possible duplicate of [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – caiovisk Feb 01 '18 at 02:15
  • You should really considering using JSON.parse first it will make your life way easier, then you can just use functions like you do over an object literal – Matthew Lagerwey Feb 01 '18 at 02:47

3 Answers3

2

You cannot use .length over an object, since it works over array.

However you can use Object.Keys.Lengh

var movies = {
  "Black Panther" : {
    "title" : "Black Panther",
    "theatricalrelease" : "2/16/2018"
  },
  "Infinity War" : {
    "title" : "Avengers: Infinity War",
    "theatricalrelease" : "5/4/2018"
  },
  "Captain Marvel" : {
    "title" : "Captain Marvel",
    "theatricalrelease" : "TBA"
  }
}

 
console.log(Object.keys(movies).length);
for(var key in movies) {
   console.log(movies[key]);
}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You can access the title and get the length in this way :

var movies = {
  "Black Panther" : {
    "title" : "Black Panther",
    "theatricalrelease" : "2/16/2018"
  },
  "Infinity War" : {
    "title" : "Avengers: Infinity War",
    "theatricalrelease" : "5/4/2018"
  },
  "Captain Marvel" : {
    "title" : "Captain Marvel",
    "theatricalrelease" : "TBA"
  }
}
var totalMovies = Object.keys(movies).length;
console.log(totalMovies);

for (var key in movies) {
    if (movies.hasOwnProperty(key)) {
        console.log( movies[key].title);
        console.log( movies[key].theatricalrelease);
    }
}
Hoque MD Zahidul
  • 10,560
  • 2
  • 37
  • 40
0

i'm late to the thread.
This will do what you want.

var movies = {
  "Black Panther" : {
    "title" : "Black Panther",
    "theatricalrelease" : "2/16/2018"
  },
  "Infinity War" : {
    "title" : "Avengers: Infinity War",
    "theatricalrelease" : "5/4/2018"
  },
  "Captain Marvel" : {
    "title" : "Captain Marvel",
    "theatricalrelease" : "TBA"
  }
}

var moviesArray = Object.keys(movies);

for (var i = 0; i < moviesArray.length; i++) {
  var index = moviesArray[i];
  console.log( "Title: " +  movies[index].title );
  console.log( "Theatrical Release: " +  movies[index].theatricalrelease );
}
Moe-Joe
  • 1,012
  • 3
  • 15
  • 27