#Pushing the right ID Product to an array
91 messages · Page 1 of 1 (latest)
looking for the right id in the array?
Ok, replace your current handleFoodClick function with this:
function handleFoodClick(foodId){
console.log("input ID: ", foodId)
let targetFoodObj = menuArray.filter(function(food){
return food.id === foodId
})[0]
console.log("outputID: ", targetFoodObj)
}
(All I've done is add a couple of console logs to it - nothing else has changed)
yeah but I still get undefined with targetFoodObj
ahh okay, yeah
To be frank, there are 2 problems here. First, you started coding without thinking about WHAT you want your program to do. Second, you gave your program a set of instructions that you don't fully understand. Don't worry, these are very common errors and can be fixed
Yeah I thought I need the ID target so I can get the right product when I click on +. Then I want to push that right product to an array.
I got the .filter from my last project and thought I needed that anyways
Ok. Let's forget about what functions we're going to use, and think about WHAT we want our program to do. Break down your task into the most basic instructions (imagine you're talking to a very small child).
yeah ok
if right product is pushed, copy paste the content to another array
*clicked
Not bad. In my opinion there are 4 basic steps to this:
- get ID of clicked button
- match clicked ID with ID of menu item in menu array
- put matched menu item from menu array into order array
- display order array on the screen
Yes, but we need to think further. Now we know what we want our program to do, we need to think about what information our program needs to do the task. So, what information does our program need?
it wants to know which food it needs to push where
How does the program know which food item to put into the order?
I am really confused how to push from 1 array to another :/
I only know how to hardcode the push to an array
You're still thinking in syntax - I think you to think in English. Forget arrays and functions and all of that. We're planning here. (We'll get to how to push stuff into arrays and do all that later, I promise).
Ok
Imagine this: I come up to you and say "put that food item on my order" - what would you say in reply?
"That's the wrong item"
Right. So I reply "number 1"
I say no problem and press number 1
But what does number 1 mean?
Yes, you know that, but does the program know that?
no
So, what information does your program need to do the task?
It needs to know which item is what?
Good, what else?
Good, so in total we need these pieces of information:
- the selected menu item
- all possible menu items
- where to put the selected menu items
yes
It may seem silly but that's how we need to think when we're programming - What do we want our program to do, and what information does our program need. If we don't know that, we have no chance
yeah it makes sense
Now, look at your code, do you have all the pieces of information you need?
You do, you're just not understanding the instructions you're giving your program
yes
I know I can write orderArray.push[]
Something like this? orderArray.push[menuArray]
You can, but before that we need to get the correct menu item that the user selects
Replace your current handleFoodClick function with this:
function handleFoodClick(foodId){
console.log("input ID: ", foodId, typeof foodId)
let targetFoodObj = menuArray.filter(function(food){
console.log("food.id: ", food.id, typeof food.id)
return food.id === foodId
})[0]
console.log("outputID: ", targetFoodObj, typeof targetFoodObj)
}
Just as before, the logic is the same, I've just added more console logs
ok yes
Do you see the problem?
Our input ID is a string, but food.id is a number
question for you: What's the difference between == and === ?
no Idea, === is absolut and = not or something
but == no idea
ahh
I have == now
Ok, both of them are comparison operators (i.e. compare this to that and tell me if they are equal). The difference is that == only checks the values, whereas === checks the types as well as the values.
I got now the WHOLE name of the thing :DDD
So I need ==
and now push targetFoodObj to the empty ARRAY
Yep. So, for troubleshooting you need to check not only the value of things, but the type of them too. typeof is very useful for that
ahh ok
Whenever you're doing any kind of comparison, always double check the variable types
yes I will do that
orderArray.push(targetFoodObj) sends the Product to the array, but I need only name and price
For now I would just leave that as it is. Better to have too much information than too little
But the name?
What about the name?
I need that to be pushed aswell
orderArray.push(targetFoodObj["name", "price"])
like this?
Is it not inside the array already?
yes
ok
now I would build a template string where I would show the clicked item
I would make an IF (orderArray > 0) ?
wait no, IF (orderArray.length > 0)
I am off for today, thanks for all your help