I want to use the input_index to represent the array number of a certain object but it fails. I want to make the code shorter. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want to use the input_index to represent the array number of a certain object but it fails. I want to make the code shorter.

I have this code and the js array has different objects so i created a function such that when a user has finished inputing the value and clicks search yhe corresponding objects are triggered but Each id in <p> id in html has its own ''if'' statement and this makes the code longer and at some point if am am to continue i might forget some part of the code . I need some help if there is a way of using loops or anything else such that i dont need to write many ''if else '' statements I will be glad if soneone helps me because i have a code am writting and it uses this syntax; Part of it looks like this const input_text=[ { User_input:'book', bg_image:'image1 Book url', path:'page path' }, { User_input :"gold", bg_image :"image2 Gold url", path:"page2 path" }, { User_input :"car", bg_image :"image3 Car url", path:"page3 path" } ]; Here is the code https://code.sololearn.com/WDs0lO76xrh9/?ref=app

25th Sep 2021, 5:45 PM
Astrav
Astrav - avatar
1 Answer
+ 1
Here is your solution input_text.forEach((e)=>{ if(e.User_input == input.value){ console.log(e.User_input, e.bg_image, e.path) } }) Change as per your requirement..I just showed you the way you asked.
25th Sep 2021, 6:10 PM
Kashyap Kumar
Kashyap Kumar - avatar