문제

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?

도움이 되었습니까?

해결책

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/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top