wallace4793 0 Posted October 10, 2019 Report Share Posted October 10, 2019 (edited) 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 IDvar 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 IDvar 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 FEEDBACKdevice.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 October 10, 2019 by wallace4793 Quote Link to post Share on other sites
Funce 42 Posted October 13, 2019 Report Share Posted October 13, 2019 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. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.