last day (15 days later) » 

8:45 PM
0
A: JSON iteration based on value using jquery

J4GI like to iterate with forEach. Rather then having to worry about indexes you can directly reference each item of the array. Using this code you can put the data you want into an array. //Get info above here var westTeams = []; var westDivision = info.sta.co[1].di; westDivision.forEach(functi...

 
Thanks for that response. I'm getting 'westTeams is not defined' though. Not sure what that is referencing now as it is defined above right? I've included the updated script in the original message.
 
J4G
Sorry, I accidentally removed the line defining that variable. It should work now.
 
This is awesome @J4G but the seed order is inaccurate. Am I missing something? I've included the output above.
Portland is actually 0 not 1
I can't initiate a chat, but can you so we can go over this in depth. Just so I can understand what is happening here? I'm confused as to what you did with the purhs. Also the order is still not correct.
 
Apparently I don't have enough rep to do this either.
 
J4G
8:45 PM
You can't join the chat? My link was wrong at first, try the new one.
 
It says I need 20 rep.
 
Perfect.
 
J4G
Alright
 
Okay so let's go from the first code block and work our way down. Is that alright?
 
J4G
8:48 PM
Yup!
So first, we create an array called westTeams which we're going to use to store data about each of our teams.
Are you familiar with lists / arrays?
 
You type faster than I do. Tht's wht I was going to say. haha
Yes I'm coming from PHP so I knew we were initializing it there.
List would be the same as array?
 
J4G
Yup
Technically JS doesn't have arrays, they have lists. But it's a technicality
Then we're getting the relevant information from the JSON file you downloaded and setting it to westDivision
 
co[1] because we just want the second set right?
 
J4G
Yup
If we wanted east we could change it to co[0]
forEach is a super useful function that is like the better cousin of for
 
Okay. Quick question. I'm typign it out now
 
J4G
8:52 PM
It lets you iterate over an array without that verbose garbage of for (i=0;i<array.length;i++)
 
Nevermind I think I answered my own question when I was typing it out.
forEach seems very useful.
 
J4G
Yup
so any array you can just use .forEach
in the format
array.forEach(function (item,index)
{

});
Where the item is each item of the array, and the index is the index. :P
The sort function is a neat feature of javascript
It lets you sort arrays very quickly with custom code
In this case, if one items seed is less than another's we "return -1" which means put it after that one
If it is greater we "return 1" which means put it before
and if neither of those are true we just leave it in place "return 0"
Finally we iterate over our new sorted array with forEach
 
Okay one second while I digest the second code block.
Okay I think I understand. It's new to me so I'm no expert yet, but I certainly understand the logic.
I need to get used to the forEach
array.forEach*
 
J4G
That's honestly just something I like to use. If you don't there's no reason you have to use it
You can go back to using for
 
No I feel like you were able to do this quicker. It makes sense to do it this way for sure. I just need more practice with it.
 
J4G
9:05 PM
So do you understand the "push" line?
 
Not quite.
 
J4G
Ok so push adds a new item to the westTeams array
In this case, there's multiple pieces of information we need in that item
So we use an object
in JavaScript objects are just fancy arrays
 
What does the push start off as?
So we're adding to it, but what does it intialize as?
 
J4G
where instead of accessing information through a number like array[0] we can use text
So west teams is a blank array []
then push adds an item
to the end of it
every time
 
Whoops. Okay I see now.
 
J4G
9:08 PM
So we want to store the city, the name, and the seed
So we throw it into an object using curly brackets
 
so city is just another part of the array right?
"city:"
 
J4G
City is a "property" of the object
 
the added property right?
from the push or is this a call to something?
 
J4G
OK, this is getting into the fundamentals of javascript
 
haha sorry.
 
J4G
9:10 PM
Nah, that's OK
So an object has properties
Where a string (text) is used to access data
{
a: 0,
b: 0
}
 
Okay so the push would be the same as if we created the json within a variable? Like if we created the object as a variable?
 
J4G
Yeah
 
Okay I see what you mean.
 
J4G
so imagine we're setting a variable teamData equal to the object
Then push just adds it to the end of the array
Here, maybe this will make things more clear
[{"city":"Golden State","name":"Warriors","seed":1},{"city":"Memphis","name":"Grizzlies","seed":2‌​},{"city":"Houston","name":"Rockets","seed":3}]
That's what part of sorted looks like
See how each item is seperated by a comma and each item is an object?
 
A useable object?
right
 
J4G
9:13 PM
If we want to figure out the name of the first item of our array, all we have to do is go sorted[0].name
That
That's pulling the first item
then accessing the name from the object
Now here's the kicker. The period followed by a string (".name") is an alternate way of accessing an object's property
I could also go sorted[0]['name'] with the exact same results
 
With limitations or just literally the exact same results?
 
J4G
See how arrays and objects are related? An array is just an object where you're accessing items with a number instead of a string
Literally exactly the same
 
I do.
 
J4G
In fact, the second one is more versatile in some situations
Since I can do something like var thePropertyIWant = 'name' sorted[0][thePropertyIWant]
That would also give the same result
So, any more questions, or did I just make things more confusing? :P
 
Haha no this is of great help. I'm trying to figure out how to download our convo so I can reference it as I practice. I really appreciate your help on this. Do you do any freelance work?
 
J4G
9:18 PM
On occasion. I don't have anything super official set up, I just help when I'm asked for it. :)
 
Well if you have an email/website or something I could use some help from time to time.
 
J4G
Shoot me an email at jack@gradebuzz.com
 
A couple of extra bucks here and there. What other languages do you know?
 
J4G
I'm fluent in JS, Python, but I've dabbled in basically every language
I've messed around with PHP a bit, Java, Visual Basic, C#, the works
But honestly I don't mind just answering your questions for free whenever you've got them
 
Awesome. I don't do much with Python, but JS I get stuck from time to time so a quick debug is available.
That would be awesome. I appreciate all of your help Jack.
 
J4G
9:21 PM
Yup absolutely. I use Stack Overflow a lot for help too, so it's good to be able to give back
 
Absolutely. Thanks so much Jack. I'm going to take off, but have a great day!
 
J4G
You too!
 

last day (15 days later) »