admin管理员组文章数量:1416631
I'm trying to write a loop for an array but getting an invalid string error.
If keyword = "mesothelioma|seo"
function json(keyword) {
var jsondata = UrlFetchApp.fetch("="+keyword);
var object = Utilities.jsonParse(jsondata.getContentText());
var results = Array("Error", "Error", "Error", "Error");
for (var i = 0; i < object.length; i++) {
results[0] = object[0].cpc;
results[1] = object[0].cmp;
results[2] = object[0].lms;
results[3] = object[0].m1;
}
return results;
}
Any thoughts?
I'm trying to write a loop for an array but getting an invalid string error.
If keyword = "mesothelioma|seo"
function json(keyword) {
var jsondata = UrlFetchApp.fetch("http://api.grepwords./lookup?apikey=carterq="+keyword);
var object = Utilities.jsonParse(jsondata.getContentText());
var results = Array("Error", "Error", "Error", "Error");
for (var i = 0; i < object.length; i++) {
results[0] = object[0].cpc;
results[1] = object[0].cmp;
results[2] = object[0].lms;
results[3] = object[0].m1;
}
return results;
}
Any thoughts?
Share Improve this question edited May 30, 2014 at 1:20 ethanenglish asked May 30, 2014 at 0:48 ethanenglishethanenglish 1,3272 gold badges16 silver badges33 bronze badges1 Answer
Reset to default 2Your reference error is ing from this line:
var jsondata = UrlFetchApp.fetch("http://api.grepwords./lookup?apikey=carter&q="+keyword);
From your above code, this is the only moment where you use 'keyword'. You sure it returns the correct information? And if it does have you thought about your loop?
Suppose your var object = Utilities.jsonParse(jsondata.getContentText());
returns this:
var object = [
{cpc: 'test1', cmp: 'test2', lms: 'test3', m1: 'test4'},
{cpc: 'test5', cmp: 'test6', lms: 'test7', m1: 'test8'},
{cpc: 'test9', cmp: 'test10', lms: 'test11', m1: 'test12'}
];
And we use your loop:
for (var i = 0; i < object.length; i++) {
results[0] = object[0].cpc;
results[1] = object[0].cmp;
results[2] = object[0].lms;
results[3] = object[0].m1;
}
You do realize you never use 'i'? and you overwrite what you had in results with the same thing after each loop? Are you sure you wanted this?
function somefunc() {
var object = [
{cpc: 'test1', cmp: 'test2', lms: 'test3', m1: 'test4'},
{cpc: 'test5', cmp: 'test6', lms: 'test7', m1: 'test8'},
{cpc: 'test9', cmp: 'test10', lms: 'test11', m1: 'test12'}
];
var results = Array("Error", "Error", "Error", "Error");
if (object[0] != undefined)
{
results[0] = object[0].cpc;
results[1] = object[0].cmp;
results[2] = object[0].lms;
results[3] = object[0].m1;
}
for (var i = 0; i < object.length; i++) {
results[0] = object[0].cpc;
results[1] = object[0].cmp;
results[2] = object[0].lms;
results[3] = object[0].m1;
}
return results;
}
console.log(somefunc());
本文标签: javascriptLoop an array in Google Apps ScriptsStack Overflow
版权声明:本文标题:javascript - Loop an array in Google Apps Scripts - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256326a2650124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论