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?

4
  • That isn't an array. Commented Feb 1, 2018 at 2:13
  • Possible duplicated question of: stackoverflow.com/questions/684672/… Commented Feb 1, 2018 at 2:15
  • 2
    Possible duplicate of How do I loop through or enumerate a JavaScript object? Commented Feb 1, 2018 at 2: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 Commented Feb 1, 2018 at 2:47

3 Answers 3

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]);
}

Sign up to request clarification or add additional context in comments.

Comments

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);
    }
}

Comments

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 );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.