TypeError:在对象中找不到forEach函数 [英] TypeError: Cannot find function forEach in object

查看:184
本文介绍了TypeError:在对象中找不到forEach函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建用于检索XML数据的缓存.

I'm trying to create a cache for retrieving XML data.

我的问题是我收到一个错误"TypeError:在对象中找不到forEach函数"

My problem is im getting an error "TypeError: Cannot find function forEach in object"

我不明白为什么我会收到错误消息,并且我从示例中复制了大部分错误消息.

I dont understand why im getting the error and i copied most of this out from an example.

错误发生在:

 priceIDs.forEach (function (row) {
 row.forEach ( function (cell) {
  if (typeof(cell) === 'number' ) {
    dirtyTypeIDs.push(cell);
  }
 });
 });

"row.forEach(function(cell){"行似乎是罪魁祸首.我将在下面发布整个代码,以帮助人们:)

The line "row.forEach ( function (cell) {" appears to be the culprit. I will post the whole of the code below for people to help :)

function LoadPrices(priceIDs, systemID,cacheBuster){

if (typeof systemID == "undefined") {
systemID=30000142;
}

if (typeof priceIDs == "undefined") {
throw "need typeids";
}

if (typeof cacheBuster == "undefined") {
cacheBuster=1;
}

var prices = new Array();
var dirtyTypeIDs = new Array();
var cleanTypeIDs = new Array();

var url = "http://api.eve-central.com/api/marketstat?cacheBuster="+cacheBuster+"&usesystem="+systemID+"&typeid=";

priceIDs.forEach (function (row) {
row.forEach ( function (cell) {
  if (typeof(cell) === 'number' ) {
    dirtyTypeIDs.push(cell);
  }
});
});

cleanTypeIDs = dirtyTypeIDs.filter(function(v,i,a) {
return a.indexOf(v)===i;
});

var parameters = {method : "get",payload: ""};

var xmlFeed = UrlFetchApp.fetch(url+cleanTypeIDs.join("&typeid="),parameters).getContent();
var xml = XmlService.parse(xmlFeed);  

if(xml)
{
var rows=xml.getRootElement().getChild("marketstat").getChildren("type");
for(var i = 0; i< rows.length; i++) {
  var price = [rows[i].getAttribute("id").getValue(),
  rows[i].getChild("sell").getChild("volume").getValue(),
  rows[i].getChild("sell").getChild("avg").getValue(),
  rows[i].getChild("sell").getChild("max").getValue(),
  rows[i].getChild("sell").getChild("min").getValue(),
  rows[i].getChild("sell").getChild("stddev").getValue(),
  rows[i].getChild("sell").getChild("median").getValue(),
  rows[i].getChild("sell").getChild("percentile").getValue(),
  rows[i].getChild("buy").getChild("min").getValue(),
  rows[i].getChild("buy").getChild("avg").getValue(),
  rows[i].getChild("buy").getChild("max").getValue(),
  rows[i].getChild("buy").getChild("min").getValue(),
  rows[i].getChild("buy").getChild("stddev").getValue(),
  rows[i].getChild("buy").getChild("median").getValue(),
  rows[i].getChild("buy").getChild("percentile").getValue(),
];
prices.push(price);
}
};
}

能帮助我理解错误并更正吗?

Can any help me understand the error and correct it?

推荐答案

简短答案:

您的row变量实际上是一个对象,因此您不能使用forEach.您可以使用以下代码正确地迭代row:

Your row variable is actually an Object, so you can't use forEach. You can use this code to iterate row properly:

for (var column in row) {
    if (row.hasOwnProperty(column)) {
        var cell = row[column];

        if (typeof cell == "number") {
            dirtyTypeIDs.push(cell);
        }
    }
}

更多信息此处.

详细答案:

TypeError 是当您尝试访问对象的属性,但该属性丢失时,会发生某些事情.

A TypeError is something that happens when you attempt to access a property of an Object, but the property is missing.

在第一种情况下,您要求priceIDs( Array )调用其forEach函数.它很高兴做到这一点,为您提供所需的结果(row).

In the first case, you ask priceIDs (an Array) to call its forEach function. It does this happily, giving you the result you need (row).

但是,在第二种情况下,您要求row(对象)调用其forEach函数. row不知道任何叫forEach的东西,因此它立即死亡.

However, in the second case, you ask row (an Object) to call its forEach function. row doesn't know anything called forEach, so it dies immediately.

要迭代对象的属性,您需要使用 for...in 循环.上面的代码将使用row,迭代适当的属性,然后根据您提供的逻辑将其值推入dirtyTypeIDs.代码下方链接的问题回答了为什么需要hasOwnProperty检查的原因.

To iterate properties of an Object, you need to use a for...in loop. The code above will take row, iterate the appropriate properties, and push their values to dirtyTypeIDs based on the logic you provided. The question linked below the code answers why the hasOwnProperty check is necessary.

这篇关于TypeError:在对象中找不到forEach函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆