Loading
Current section: 6. Object Methods 4 exercises
solution

Keys Values

Transcript

00:00 Alright. Let's go through this together. So we've got our inventory, apples, oranges, bananas, and mangoes. There are no bananas. That's sad.

00:07 But we're going to use object dot keys to get an array of the item name. So this is, the object keys method. This is gonna give us our item names for the inventory. So it's basically gonna give us an array of each one of these in, in order. And so let's, go ahead and uncomment that.

00:27 We'll see we've got apples, oranges, bananas, mangoes. Tada. It's pretty simple. Hopefully pretty straightforward. And then we can do the same for the values.

00:35 Get just an array of the values. Tada. Twelve, eight, four, zero and four. That could be pretty useful sometime. And then we can use that fund reduce method to get the total quantity based off of our quantities.

00:50 So add all of those together. Of course, you could do the same with the original, inventory, or actually, yeah, inventory is not an array. So, no, this is, like, this is how you would do this is how I would do this. You can, of course, do a loop instead. So, we could do for of, on or our, inventory.

01:12 Yeah. Inventory. Oh my goodness. And we yeah. We'd get the values to do a for of.

01:19 Or you could do a for in key in object. So I can get our inventories, and let's do our let total quantity and get rid of that. There we go. See, types get kinda funny when we're doing stuff like this, but that would also technically work. Personally, I like object values with, the reduce in this case, and so we're gonna just go with that.

01:45 Or you could do, object values, and do a for of. I honestly don't use the for in loop very much. But if you want to, you can go back and pause the video where we had the for in and kind of study that. But I honestly, I really don't use the for in very much at all. One method that we did not talk about in this one, is the entries.

02:07 We'll talk about that here in just a second. That's actually the next exercise but I'll just show you really quick because in this situation, I would actually probably or there's, I often will use a for of loop with entries, and that's gonna get us our key and quantity. And then you can do something with the key. But we don't need the key right now so the values was just fine. So let's talk about, the entries in the next exercise.

02:33 Hopefully, this was helpful and interesting, and, you had a good time with this one. We'll see you in the next.