Search code examples
jsonstringgoogle-books

string all titles from Google Books API json


I'd like to get all titles from a shelf in Google Books and produce a string of the titles separated by commas. Here's my attempt:

var allTitles = [];

$.getJSON("https://www.googleapis.com/books/v1/users/115939388709512616120/bookshelves/1004/volumes?key=MYAPIKEY",function(data){

     $.each(data.items, function (i, item) {

         allTitles.push(data.items[0].volumeInfo.title);

  });

var newTitleString = allTitles.join(', ');
    alert(newTitleString);   

http://jsfiddle.net/nathanbweb/YzXLW/

How do I make this work?


Solution

  • OK, I was able to figure it out from this answer. Here's what's working:

    $.getJSON("https://www.googleapis.com/books/v1/users/115939388709512616120/bookshelves/1004/volumes?key=MYAPIKEY",function(data){
    
        var titles = [];
    
        data.items.forEach(function(e,i){titles.push(e.volumeInfo.title);});
             console.log(titles);
    
         var newTitleString = titles.join(', ');
             console.log(newTitleString);        
    
      });
    

    http://jsfiddle.net/nathanbweb/YzXLW/3/