Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS Making Your Own Widget Start with the Data

17,917 Points

Question about data modeling from results.json

In the video, he talks about results.json structure that looks like:

{ "1":{ "javascript":40 }, "2":{ ... } }

So, why not use an array?

I'm very interested on learning how to model data in a proper way(how know how to build data models that can scale, and change in the future)

Any tips on any course here to help me with that?

1 Answer

Blake Lieberman
Blake Lieberman
23,772 Points

@jrabello Great question.

To answer that question I will first address 2 things. Both of which you may be familiar with already

1) What does JSON stand for? 2) The difference between an array and an object.

1) --> JSON stands for JavaScript Object Notation. The key phrase is Object Notation. You should expect a response of an array of objects.

Ex. [{key:element},{key:element},{key:element}]

-- An array is an list, and an object consists of key pairs.

Array = [l1,l2,l3,l4];
Object = {
  key1 : element1,
  key2 : element2

An Object can contain an array, a number, a string, a boolean, or another object.

In order to find a value in an array you would have to know its place in the array (similar to a position in line). To find a value in an object you would need to know the value of the key (similar to yelling out the name of a person waiting in line).

Iterating through the entire array (similar to walking through a line to look for the person you are searching for) to find an element if you don't know its location is very tedious, isn't scalable. Why I feel it isn't scalable is if you had an array of 10000 elements. Iterating through the entire array to find the element would take a bit of time - in terms of computer processing (every second counts).

Compare this to an object where we know only the name of the key to access the element we want. We just call that key pair ex. obj.key1 to access element1.

When we send an http request to JSON we get a response that is organized the same way we organize objects in javascript.

Let me know if you have more questions.

17,917 Points

Thanks Blake,

I understand now, so he just made the results.json in a way it doesn't need to loop to get the desired object, it just access it using the key, if there is an unique key identifier it can be used to keep data into sync, GREAT!

I thought when we accessed an object with a key js interpreter was responsible for looping until finding the object, but I think it must be implemented in a way that looks like a hash table, so we got O(1) algorithm complexity