且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从jQuery中的JSON值创建字符串

更新时间:2022-10-15 09:19:33

从称为list的ISBN字符串数组开始:

list.map(function(v){return "isbn:"+v;}).join("+OR+")

关于建立您的ISBN列表,我理解是您的industryIdentifiers中的identifier道具(如果industryIdentifier是真诚的Array):

var list = [];
volumeInfo.industryIdentifiers.forEach(function(e,i){list.push(e.identifier);});

您也可以一次构建最后一个字符串,而根本不构建数组,但这意味着需要额外的逻辑来防止插入额外的定界符(+OR+是定界符)

var output = "";
volumeInfo.industryIdentifiers.forEach(function(e,i){output += "isbn:"+e.identifier+"+OR+";});
output.slice(0,-4); // clear out last +OR+

I'd like to create a string of ISBNs from a JSON object to search multiple books in Google Books.

I can get the ISBNs by parsing Google Books' JSON with this path:

volumeInfo.industryIdentifiers[0].identifier

(Here I'm doing that to get a simple book list: jsfiddle.net/LyNfX )

How do I string together each value in order to get this query structure, where each ISBN is preceded by "isbn:", and after the first one they are separated by "OR"

https://www.google.com/search?btnG=Search+Books&tbm=bks&q=Springfield+isbn:1416549838+OR+isbn:068482535X+OR+isbn:0805093079+OR+isbn:0306810328

Starting from an array of ISBN strings called list:

list.map(function(v){return "isbn:"+v;}).join("+OR+")

As for building your list of ISBN's which I understand to be the identifier prop in your industryIdentifiers (if industryIdentifier is a bona fide Array):

var list = [];
volumeInfo.industryIdentifiers.forEach(function(e,i){list.push(e.identifier);});

You could also just build the final string in one fell swoop and not build an array at all, but this means extra logic to prevent inserting an extra delimiter (+OR+ being the delimiter)

var output = "";
volumeInfo.industryIdentifiers.forEach(function(e,i){output += "isbn:"+e.identifier+"+OR+";});
output.slice(0,-4); // clear out last +OR+