javascript - Loop an array in Google Apps Scripts - Stack Overflow

I'm trying to write a loop for an array but getting an invalid string error.If keyword = "mes

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 badges
Add a ment  | 

1 Answer 1

Reset to default 2

Your 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());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745256327a4618979.html

相关推荐

  • javascript - Loop an array in Google Apps Scripts - Stack Overflow

    I'm trying to write a loop for an array but getting an invalid string error.If keyword = "mes

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信