Jump to content

"For Loop" - incrementing a device name?


wallace4793
 Share

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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...