Jump to content
Sign in to follow this  
wallace4793

"For Loop" - incrementing a device name?

Recommended Posts

Hi,

Im  learning a little JS to use a home automation application that requires some low level coding, namely JS. I have what is probably a basic question. I want to test using a "for" loop and increment part of the device object name too. Is this possible. 

For example, my working code is :

recent1id = (searchresult.result.movies[0].movieid) // EXTRACT MOVIE ID
var r = http.get(('https://api.themoviedb.org/3/search/movie?api_key=') + (tvdbid) + ('&query=') + (searchresult.result.movies[0].title)) // EXTRACT MOVIE DETAILS FROM TVDB
 var json = r.data; // RECEIVE FEEDBACK
device.MovieThumb1 = ("http://image.tmdb.org/t/p/original" + (json.results[0].poster_path));    // ASSIGN POSTER                
device.MovieTitle1 = (searchresult.result.movies[0].title)
                    

recent2id = (searchresult.result.movies[1].movieid) // EXTRACT MOVIE ID
 var r = http.get(('https://api.themoviedb.org/3/search/movie?api_key=') + (tvdbid) + ('&query=') + (searchresult.result.movies[1].title)) // EXTRACT MOVIE DETAILS FROM TVDB
 var json = r.data; // RECEIVE FEEDBACK
 device.MovieThumb2 = ("http://image.tmdb.org/t/p/original" + (json.results[0].poster_path));    // ASSIGN POSTER
 device.MovieTitle2 = (searchresult.result.movies[1].title)

 

The 2 portions of code simply extract a movie title and pull the images from TMDB and assign to my device object - is it possible to increment the process using the for loop? How I imagine it might work is below..... where variable "i" replaces the values I want to increment through the loop.

for (i = 0; i < 5; i++) {
 recent[i+1]id = (searchresult.result.movies[i].movieid) // EXTRACT MOVIE ID
var r = http.get(('https://api.themoviedb.org/3/search/movie?api_key=') + (tvdbid) + ('&query=') + (searchresult.result.movies[i].title)) // EXTRACT MOVIE DETAILS FROM TVDB
 var json = r.data; // RECEIVE FEEDBACK
device.MovieThumb[i +1] = ("http://image.tmdb.org/t/p/original" + (json.results[i].poster_path));    // ASSIGN POSTER                
device.MovieTitle[i  + 1] = (searchresult.result.movies[i].title)
}

 

Be kind.. its my first post 🙂

Edited by wallace4793

Share this post


Link to post
Share on other sites

You may want to re copy your code using the forum code block feature. Your [ i ] in your code has been eaten by the formatter.

XcQfW3F.png

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...