最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论