Basic loop for using (javascript) with condition -


need problem. let have object , array list on javascript below:

     var hotel={  "electronic":[        {  "id":1,        "tv":"lg"},      {  "id":2,        "tv":"polytron"},      {  "id":3,        "tv":"panasonic"},      {  "id":4,        "tv":"sony"},        ]      }      var mylist=[1,2,1,4,2,3,2,4];      var mytran;      var i=0;              while(i<mylist.length){      if(mylist[i]===1){      mytran=hotel.electronic[0].tv;      }      else if(mylist[i]===2){      mytran=hotel.electronic[1].tv;      }      else if(mylist[i]===3){      mytran=hotel.electronic[2].tv;      }      else if(mylist[i]===4){      mytran=hotel.electronic[3].tv;      }      console.log(mytran);      i++;      }      

the above code working, object data db have 4 object inside array 'electronic'. concern how if have hundreds object on electronic data array. have hundreds of if else well. think still can optimize code? please explaination.

you this:

mytran=hotel.electronic[mylist[i]-1].tv; 

i.e. take value of mylist[i] index


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -