-1

I need to assign the genre field to a new array but I am not sure how to only get that field, or how to call that field

var SpotifyWebApi = require('spotify-web-api-node');
var spotifyApi = new SpotifyWebApi();
spotifyApi.setAccessToken('-----');

 spotifyApi.searchArtists('artist:queen')
   .then(function(data) {
     console.log('Search tracks by "queen" in the artist name', data.body.artists.items);
   }, function(err) {
     console.log('Something went wrong!', err);
   });

This is the terminal when calling it. I only want the first response.

 PS C:\Users\g\Documents\js> node spotifyTest
Search tracks by "queen" in the artist name [ { external_urls:
     { spotify: 'https://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d' },
    followers: { href: null, total: 19579534 },
    genres: [ 'glam rock', 'rock' ],
    href: 'https://api.spotify.com/v1/artists/1dfeR4HaWDbWqFHLkxsg1d',
    id: '1dfeR4HaWDbWqFHLkxsg1d',
    images: [ [Object], [Object], [Object], [Object] ],
    name: 'Queen',
    popularity: 90,
    type: 'artist',
    uri: 'spotify:artist:1dfeR4HaWDbWqFHLkxsg1d' },
  { external_urls:
     { spotify: 'https://open.spotify.com/artist/3nViOFa3kZW8OMSNOzwr98' },
    followers: { href: null, total: 1087117 },
    genres: [ 'deep pop r&b', 'pop', 'r&b' ],
    href: 'https://api.spotify.com/v1/artists/3nViOFa3kZW8OMSNOzwr98',
    id: '3nViOFa3kZW8OMSNOzwr98',
    images: [ [Object], [Object], [Object] ],
    name: 'Queen Naija',
    popularity: 68,
    type: 'artist',
    uri: 'spotify:artist:3nViOFa3kZW8OMSNOzwr98' } ]
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
gpjs
  • 47
  • 1
  • 11
  • 1
    Possible duplicate of [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Kevin B Sep 18 '19 at 18:41

1 Answers1

-1

You can access a field in a JSON object using the dot notation. Here's an example of replacing the genres of the first response with a new array.

let responseItems = [ 
  { external_urls: { spotify: 'https://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d' },
    followers: { href: null, total: 19579534 },
    genres: [ 'glam rock', 'rock' ],
    href: 'https://api.spotify.com/v1/artists/1dfeR4HaWDbWqFHLkxsg1d',
    id: '1dfeR4HaWDbWqFHLkxsg1d',
    images: [ [Object], [Object], [Object], [Object] ],
    name: 'Queen',
    popularity: 90,
    type: 'artist',
    uri: 'spotify:artist:1dfeR4HaWDbWqFHLkxsg1d' 
  },
  { 
    external_urls: { spotify: 'https://open.spotify.com/artist/3nViOFa3kZW8OMSNOzwr98' },
    followers: { href: null, total: 1087117 },
    genres: [ 'deep pop r&b', 'pop', 'r&b' ],
    href: 'https://api.spotify.com/v1/artists/3nViOFa3kZW8OMSNOzwr98',
    id: '3nViOFa3kZW8OMSNOzwr98',
    images: [ [Object], [Object], [Object] ],
    name: 'Queen Naija',
    popularity: 68,
    type: 'artist',
    uri: 'spotify:artist:3nViOFa3kZW8OMSNOzwr98' 
  } 
];
let firstResponse = responseItems[0];
console.log(JSON.stringify(firstResponse.genres, null, 2));
let newGenres = [ 'rock', 'jazz' ];
firstResponse.genres = newGenres;
console.log(JSON.stringify(firstResponse.genres, null, 2));

This should show the following in the console:

[
  "glam rock",
  "rock"
]

[
  "rock",
  "jazz"
]
Harvard Pan
  • 79
  • 1
  • 4