If you're trying to access data from a JSON sample, simply deserializing it won't give you hash-style access.
One approach could be re-structuring the JSON and using object literals for your shows:
{
"shows": {
"House of cards": {
"rating": 8
}
}
}
Afterwards, you can get an array of show keys by using Object.keys(...)
:
Object.keys(x.shows);
You also have the option to modify the structure post deserialization:
var x = { shows: {} };
for(var index in some.shows) {
x.shows[some.shows[index].name] = { rating: some.shows[index].rating };
}
// Accessing a show
var rating = x.shows["House of cards"].rating;
I recommend converting the data in this way to enable easy plain JavaScript access to your shows, without requiring full iteration through the show array.
By utilizing object literals, accessing properties becomes similar to a dictionary or hash table method, eliminating any need for a search function in the background.
Update
If you are wondering how to iterate through shows once they are in an associative array/object format instead of a regular array:
Object.keys(shows).forEach(function(showTitle) {
// Perform actions for each iteration here
});
Alternatively...
for(var showTitle in shows) {
// Perform actions for each iteration here
}
Update 2
For a functional example, check out this jsFiddle demo: http://jsfiddle.net/dst4U/