Trying to download a 2D array as .csv, but I'm getting a file back for each row

Here is my code:


function getRelease(idFiltered) {
  return fetch(`https://api.discogs.com/releases/${idFiltered}`, {
    'User-Agent': 'Dispodger/0.1',
  })
  .then(response => response.json())
  .then(data => {
    if (data.message === 'Release not found.') {
      return { error: `Release with ID ${idFiltered} does not exist` };
    } else {
      const id = data.id;
      const artists = data.artists ? data.artists.map(artist => artist.name) : [];
      const country = data.country || 'Unknown';
      const released = data.released_formatted || 'Unknown';
      const genres = data.genres || [];
      const styles = data.styles || [];
      const tracklist = data.tracklist ? data.tracklist.map(track => track.title) : [];

      console.log(idFiltered);
      console.log(artists, country, released, genres, styles, tracklist)

      const rows = [
        [idFiltered, artists, country, released, genres, styles, tracklist],

      ];

      const ROW_NAMES = ["Release ID", "artists", "country", "released", "genres", "styles", "tracklist"];

      console.log(rows);

      let csvContent = "data:text/csv;charset=utf-8,"
      + ROW_NAMES + "\n" + rows.map(e => e.join(",")).join("\n");

      console.log(csvContent);

      var encodedUri = encodeURI(csvContent);
      var link = document.createElement("a");
      link.setAttribute("href", encodedUri);
      link.setAttribute("download", "my_data.csv");
      document.body.appendChild(link); // Required for FF

      link.click();

    }
  });
}

It’s pretty close to what I want, but I’m getting back one file per line of output, instead of appending each line to a single file. From what I can see, there seems to be a number of different ways to join the data into a csv file, like using a for loop, or forEach() method, with push() method to push each item into a new array.

I suppose the logic of what I’m trying to do is…

  • Get the various data items for each release specified in the input file
  • Join each data item into an array for each release
  • Combine these arrays into a 2D array, separated by line breaks
  • Output them preceded with the row headers followed by a line break

Thanks in advance for any pointers.

Edit: the output should be something like this:

   Release ID,artists,country,released,genres,styles,tracklist\n
   1,The Persuader,Sweden,Mar 1999,Electronic,Deep House,Östermalm,Vasastaden,Kungsholmen,Södermalm,Norrmalm,Gamla Stan\n
   2,Mr. James Barth & A.D.,Sweden,Jun 1998,Electronic,Broken Beat,Techno,Tech House,A Sea Apart,Dutchmaster,Inner City Lullaby,Yeah Kid!\n
   3,Josh Wink,US,13 Jul 1999,Electronic,Techno,Tech House,Untitled 8,Anjua (Sneaky 3),When The Funk Hits The Fan (Mood II Swing When The Dub Hits The Fan),What's The Time, Mr. Templar,Vol. 2,Political Prisoner,Pop Kulture,K-Mart Shopping (Hi-Fi Mix),Lovelee Dae (Eight Miles High Mix),Sweat,Silver,Untitled,Boom Box,Track 2\n
   4,Faze Action,US,02 Nov 1999,Electronic,House,Deep House,Moving Cities,To Love Is To Grow,Isis,Heartbeat,Samba,Got To Find A Way,Kariba,Space Disco,In The Trees,Turn The Point (U.S. Version),Mas,Horizons\n
   5,Datacide,Germany,15 Jan 1995,Electronic,Abstract,Ambient,Downtempo,Flashback Signal,Flowerhead,Deep Chair,So Much Light,Sixties Out Of Tune\n

Now, I say something like that, because that is still not going to solve the problem that each comma-separated value is going to be in its own cell, whereas I actually want the genres, styles and tracklist for each data item to be combined into single cells, like this:

[Release ID][artists][country][released][genre 1, genre 2,…][style1, style 2,…][track1, track 2,…]

(where [] is a cell)

But I’m really not ready to try and solve that problem until I’ve got the output into a single file. If I try to solve two problems at once I’m almost bound to get into a muddle.

The docs of the site I’m accessing are at https://www.discogs.com/developers.

This is what I'm logging in the console

131 thoughts on “Trying to download a 2D array as .csv, but I'm getting a file back for each row”

Leave a Comment