Accessing response.json data arrays

I’ve looked everywhere but cannot find the answer for this problem.

RAWG is a game review website now with their own API (instead of using Rapid API) and I want data for “Tom Clancy Rainbow Six Siege”.

I convert the data returned from the request into a response.json (using the code below) but I cannot access the game “Tom Clancy Rainbow Six Siege”.

fetch("https://api.rawg.io/api/games?key=[KEY]&developers=ubisoft", {
    method: 'GET'
})

// Gets the response from the website
.then(response => response.json())
// Uses the data in the json to populate the website with information
.then(data => {
  console.log(data);
  for (const game in data.results) {
    if (game == 6) {
        console.log(game[0]['id']);
    }
  }
});

API return (this is from console.log):

{count: 305, next: 'https://api.rawg.io/api/games?developers=ubisoft&key=[key]&page=2', previous: null, results: Array(20), user_platforms: false}
count: 305
next: "https://api.rawg.io/api/games?developers=ubisoft&key=[key]&page=2"
previous: null
results: Array(20)
0: {slug: 'far-cry-3', name: 'Far Cry 3', playtime: 16, platforms: Array(5), stores: Array(4), …}
1: {slug: 'for-honor', name: 'For Honor', playtime: 7, platforms: Array(3), stores: Array(4), …}
2: {slug: 'watch-dogs', name: 'Watch Dogs', playtime: 19, platforms: Array(6), stores: Array(6), …}
3: {slug: 'far-cry-5', name: 'Far Cry 5', playtime: 19, platforms: Array(3), stores: Array(3), …}
4: {slug: 'assassins-creed-odyssey', name: "Assassin's Creed Odyssey", playtime: 32, platforms: Array(4), stores: Array(5), …}
5: {slug: 'assassins-creed-origins', name: "Assassin's Creed Origins", playtime: 35, platforms: Array(3), stores: Array(3), …}
6: {slug: 'rayman-legends', name: 'Rayman Legends', playtime: 5, platforms: Array(8), stores: Array(6), …}
7:
added: 5061
added_by_status: {yet: 93, owned: 3811, beaten: 244, toplay: 56, dropped: 533, …}
background_image: "https://media.rawg.io/media/games/b34/b3419c2706f8f8dbe40d08e23642ad06.jpg"
clip: null
dominant_color: "0f0f0f"
esrb_rating: {id: 3, name: 'Teen', slug: 'teen', name_en: 'Teen', name_ru: 'С 13 лет'}
genres: (2) [{…}, {…}]
id: 8488
metacritic: 75
name: "Tom Clancy's Rainbow Six Siege"
parent_platforms: (3) [{…}, {…}, {…}]
platforms: (5) [{…}, {…}, {…}, {…}, {…}]
playtime: 29
rating: 3.99
rating_top: 4
ratings: (4) [{…}, {…}, {…}, {…}]
ratings_count: 1076
released: "2015-12-01"
reviews_count: 1085
reviews_text_count: 5
saturated_color: "0f0f0f"
score: null
short_screenshots: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
slug: "tom-clancys-rainbow-six-siege-2"
stores: (4) [{…}, {…}, {…}, {…}]
suggestions_count: 466
tags: (16) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
tba: false
updated: "2021-02-17T15:00:09"
user_game: null
[[Prototype]]: Object

I want to access #7 (Rainbow six siege) and get the id and name. But for the life of me I can’t figure it out. The for loop just returns the number 6, and I can’t access any data inside this.

Answer

OK, with a bit of digging I came up with this.

Your search parameters should be search= the title you’re looking for, and search_exact which should be true.

search=tom-clancys-rainbow-six-siege&search_exact=true

It should return data of count 1, and you can access the information in the object of the first results array element.

const url = 'https://api.rawg.io/api/games?developers=ubisoft&key={your key}&search=tom-clancys-rainbow-six-siege&search_exact=true';

async function getData() {
  const json = await fetch(url);
  const data = await json.json();
  const { id, name } = data.result[0];
  console.log(id, name);
}