Transcript
00:00 Okay. Let's make this product inventory, arrays of objects. So first, we wanna make a type. So we're gonna make our product product type, which has a name, price, and in stock. And then we want to make, an array, a variable called products with, two to three products.
00:20 And so here we go. We've got a couple products. Now here, our AI assistant has been trained by all the bad code that other people have written, and is using the array syntax that doesn't use the array generic. I do not like this shorthand syntax. I think it's confusing.
00:39 It's, as I'm reading it, it's product. Oh, an array of products. No, no, no. I want an array of what? Products.
00:46 Oh, okay. Great. That is so much better and especially if we have like it could be a product or it could be a user. Well, now I gotta do user. That's super annoying.
00:58 How about we just do, an array of products or users. Right? Like, there's there's it's just so much better. I know it's more typing. I know it's more typing.
01:09 It's But, yeah, it's just so much better. You will find that, when I'm heavily relying on an AI agent though, I typically don't care enough to like correct the AI agent. The agent gets it pretty well. Though I'll be honest, I think that if it's easier for humans to understand, it's probably easier for agents to understand as well. So I'm gonna prefer this as much as possible and I'm gonna tell my agent that this is what I prefer as well.
01:36 Okay. Great. And now that we've dealt with that, we've got a laptop, mouse, and keyboard. Those prices look pretty reasonable. Right?
01:46 So that's good. Let's clean up this a little bit, get a little bit more, visual space here. And we're gonna add another product. This has to have the same shape as everything else. If we were to change any one of these, we're gonna get an error.
02:02 It's saying, hey, in stock can't be a string. And it we're we can say, hey. Price can't be true. And so this is the benefits of being able to type all of this stuff. So we add the new product.
02:14 Now let's log the name of the first product. So this is like chaining properties. Now remember, objects are basically a basket of other things inside of them, and arrays are also an object. So with that index zero, we're gonna grab the zero with item and oh, that turns out to be a basket also. So now let's grab into that basket and grab the name property.
02:36 That's what's going on right here. So, grab the products, grab the zeroth item out of the products and grab the name out of that product. And then we can also do, the last one. And, so that's kinda fun. Let's go ahead now and do a loop.
02:54 So yeah. Total value. That'll work. Iterate over all of those and we get our, total value of all of the products in there. We can do, this for of loop.
03:05 You could also do a traditional, for let, products loop if you want to do it that way too. Either one works just fine. Personally, I like the for of, because I think that it's just a little bit more straightforward. You don't have to do the, you know, products bracket, thing there. You just get a variable and it's kinda nice there.
03:26 But however you wanna do it and sometimes you actually do need the index and it's just easier to have the four loop with that, with that index. And actually, let's back it up. Let's let's explore this a little bit just to review the four loop and and why what makes this beneficial now with the added context of what an array is. So here of course, this is our initializer. We're initializing the state of this block and so we're gonna have a variable called I.
03:51 If we wanted to be a little more specific, I'd rename it to index And so, we have our index of the item. We're gonna start with zero because that's, how arrays are structured. It starts with a zero index item. And then as long as our index is less than the product length, then there's another product in there we wanna, access. So then we're gonna say index plus plus.
04:12 Increment, the that index. So we move on to the next one. And then we're gonna take the total value, we'll plus equal that to the price of the product at the current index. So we'll grab whatever that index is, grab the product from there, and, get the price from that object and add that to the total value. And then we end up with our total value, which ultimately ends up being 1,369 and 96¢.
04:39 So that's great. Alright. There you go. That's, typed arrays with, objects. We do this all the time in JavaScript.
04:48 Like, all the time, arrays and, and objects, they're like buddy buddy. They're really good friends. So hopefully that was interesting and exciting to you. We'll see you in the next one.
