I am trying to get my head around the syntax required to return an element within an object by the value associated with a key in Javascript. For instance, referring to the meal-maker exercise
(https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker), how would I call the details of a dish? If I had an appetizer called ‘Gloop’, how could I get all of the properties associated with this dish with a specified reference to ‘Gloop’?
If I know the key-value pair, how could I pull that element out of an object array? (I am not sure if my syntax is correct in describing this problem…)
Ahaha! I see… I was being daft. The ISBN is the key to the element in the object (is this the right way of saying this?), as well as a value within the element paired with the key ‘isbn’.
Thank you.
So to find a value within an object element (I am sure this is not the right way to say this…), is it necessary to iterate over the elements associated with a key?
A similar data structure for the menu exercise would be:
Ok. Thank you very much. I think this has helped me clear up a lot of confusion.
Just to be clear, if I had a long list of items in an object which contained people’s information and I wanted to use one value to find another value within that list, would I need to iterate through the items, match the value I entered and then use the index of the matched item to extract the other value?
If you have two lists of equal length and they correspond with each other then yes, iterate one list to find the term, and use its index to access the other list for the corresponding element.
Ok. I am going to try to build a function that can search a specified key within an object for a value and return another value from another specified key… I am getting tied in knots. I am sure there must be a simple way! I will leave it for tonight though.
Now to try to figure out how it works… can’t quite work out what the .price is doing on the end. Guess just learn it for now and hope it makes sense later!
If you mean multiple keys then … that’s multiple lookups
If you want to store multiple things then that’s the kind of thing you’d use an array for. And if you then wanted to use those multiple things, that’s what loops are for.
Alternatively if you wanted to use only a single lookup then use a data structure that isn’t nested.
The problem you’re imagining where you’d want multiple lookups as one key is probably instead solved by sending in the relevant part instead of some outer-most global object along with a long list of keys to get to where the relevant data is. Or holding on to something with a variable.