使用循环返回嵌套的json值(google电子表格)
returning nested json values using loop (google spreadsheet)
我有一个谷歌电子表格脚本,它可以提取json格式的书籍数据。我显示书名和作者没有问题,但"offerData"对象可以根据书籍包含不同数量的元素(卖家的价格)。现在我创建了一个循环,并存储offerData值,如下所示:
price[i] = offerdata.offers[i]["price"];
condition[i] = offerdata.offers[i]["condition"];
seller[i] = offerdata.offers[i]["seller"]["displayName"];
并返回这样的数据:
var resultRow = [[title, specstag, price[0], condition[0], seller[0], price[1], condition[1], seller[1], price[2], condition[2], seller[2]]];
显然,这只会返回3个卖家的价格,条件,卖家信息。问题是,一本书并不总是有3个卖家,它可以是1到10个左右
我的问题是如何在此处返回所有offerData(价格/条件/卖家)?:
var resultRow = [[title, specstag, price[0], condition[0], seller[0], price[1], condition[1], seller[1], price[2], condition[2], seller[2]]];
--
function getBookDetails(isbn) {
// Query the book database by ISBN code.
if (isbn !== "") {
var url = "https://api.bol.com/catalog/v4/search/?apikey=myapi6&offers=all&format=json&q=" + isbn;
var response = UrlFetchApp.fetch(url);
var results = JSON.parse(response);
if (results.totalResultSize) {
// There'll be only 1 book per ISBN
var book = results.products[0];
// get Title and Authors
var title = (results.products[0]["title"]);
var specstag = (results.products[0]["specsTag"]);
var offerdata = results.products[0]["offerData"];
if (typeof offerdata.offers !== 'undefined' && offerdata.offers.length > 0) {
var arrayLength = offerdata.offers.length;
var price = [];
var condition = [];
var seller = [];
for (var i = 0; i < arrayLength; i++) {
price[i] = offerdata.offers[i]["price"];
condition[i] = offerdata.offers[i]["condition"];
seller[i] = offerdata.offers[i]["seller"]["displayName"];
}
}
}
var resultRow = [[title, specstag, price[0], condition[0], seller[0], price[1], condition[1], seller[1], price[2], condition[2], seller[2]]];
return resultRow;
}
}
答案
var resultRow = [];
resultRow[0] = [];
resultRow[0][0]=title;
resultRow[0][1]=specstag;
for (var i = 0; i < arrayLength; i=1+3) {
resultRow[0][3*i+2]=price[i];
resultRow[0][3*i+3]=condition[i];
resultRow[0][3*i+4]=seller[i];
}
您应该如何考虑它,就是查看数组中元素的索引,然后找到i和您想要的索引之间的关系
var resultRow = [
[
title, //[0][0]
specstag, //[0][1]
price[0], //[0][2]
condition[0], //[0][3]
seller[0], //[0][4]
price[1], //[0][5]
condition[1], //[0][6]
seller[1], //[0][7]
price[2], //[0][8]
condition[2], //[0][9]
seller[2]//[0][10]
]
];
您可能正在寻找这样的东西;
var data = { title: null,
spcstag: null,
pcs: [],
};
offerData.offers.forEach( p => { var pcsData = {};
!!data.title || data.title = p.title;
!!data.specstag || data.specstag = p.specstag;
pcsData.price = p.price;
pcsData.condition = p.condition;
pcsData.seller = p.seller;
data.pcs.push(pcsData);
});
相关文章:
- Google电子表格getValue([cell containing ])不返回制表符
- 使用公共API访问(无OAuth)从JavaScript编写Google电子表格
- 时间驱动触发器在 Google 电子表格中不起作用
- jQuery检查自定义Google电子表格页面上输入和下拉列表的更改
- 评估作为参数传递给 Google 电子表格中自定义函数的条件
- 将Google电子表格转换为HTML表格
- 使用循环返回嵌套的json值(google电子表格)
- 设置“;将标签视为文本“;通过脚本将Google电子表格图表的值设置为true
- 如何更改编辑单元格上方单元格的背景颜色(Javascript、Google电子表格)
- 使用Javascript和html从Google电子表格中填充html代码
- Google 电子表格脚本 - 仅授予对一张工作表的访问权限
- 监控 Google 电子表格单元格值并撤消不需要的更改
- 课程会话在某些 Google 电子表格中不起作用
- Google 电子表格:如何使用 Google Apps 脚本命名范围
- Google 电子表格脚本 getValues - 强制使用 int 而不是字符串
- Angular 将 Json 传递给 google 电子表格 getPost 抛出 e = undefined
- 将“加载更多”添加到从Google电子表格中通过$.getJSON接收的数据输出中
- 在 Google 电子表格数据库中以编程方式监视编辑
- 使用脚本中的数据,通过各种形式或提醒框修改 Google 电子表格中的数据
- 基于 Google 电子表格中公式更改的电子邮件脚本代码