Javascript Angular2 forEach 无法读取属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43914703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular2 forEach cant read property
提问by maria
How can I call this.firmswithin a data forEach()?
如何this.firms在数据中调用forEach()?
I know how to do this in Angular1, but not with my current project in Angular 2.
我知道如何在 Angular1 中执行此操作,但不知道如何在 Angular 2 中使用我当前的项目。
Currently it works fine outside of the forEach, but not within.
目前它在 forEach 之外可以正常工作,但在内部不行。
console.log(this.firms[0].name); // works
var a = 0;
console.log("--------------");
data.forEach(function (eachObj) {
console.log("firms check!");
console.log(this.firms); // not working
a = a + eachObj.income;
eachObj.name = this.firms[data.firmid - 1].name; // wont work
});
Error:
错误:
Cannot read property 'firms' of undefined
回答by Erazihel
The context thisis not injected in the anonymous function called by the forEach(). That's why thisis undefined.
上下文this未注入到 调用的匿名函数中forEach()。这this就是未定义的原因。
You can either use the arrow functionif you're using ES6 features, as it keeps the context in the function:
您可以使用arrow function,如果你正在使用ES6的特性,因为它使在功能的背景下:
data.forEach(eachObj => {
console.log("firms check!");
console.log(this.firms);
a = a + eachObj.income;
eachObj.name = this.firms[data.firmid - 1].name;
});
Or simply bind the context directly:
或者直接绑定上下文:
data.forEach(function (eachObj) {
console.log("firms check!");
console.log(this.firms);
a = a + eachObj.income;
eachObj.name = this.firms[data.firmid - 1].name;
}.bind(this));
Edit:
编辑:
As mentioned by zeroflagL, you could simply pass the context to the forEach():
正如zeroflagL所提到的,您可以简单地将上下文传递给forEach():
data.forEach(function (eachObj) {
console.log("firms check!");
console.log(this.firms);
a = a + eachObj.income;
eachObj.name = this.firms[data.firmid - 1].name;
}, this);
回答by Acika00mk
You can try to make the datato be and array
您可以尝试使datato be 和 array
like so :
像这样:
Array.from(data).forEach((eachObj) => {
console.log("firms check!");
console.log(that.firms);
eachObj.name = that.firms[data.firmid - 1].name;
})
This will work as well
这也将起作用
回答by lexith
That's the basic example for scope in javascript.
Inside your function thisrefers to the context of the function itself. The outside world isn't accessable.
这是 javascript 中作用域的基本示例。函数内部this是指函数本身的上下文。外部世界无法访问。
Since you're using Typescript with angular you can just use an arrow function:
由于您使用的是带有 angular 的 Typescript,因此您可以只使用一个arrow function:
data.forEach((eachObj) => {
console.log("firms check!");
console.log(this.firms); // not working
a = a + eachObj.income;
eachObj.name = this.firms[data.firmid - 1].name; // wont work
});
This will preserve the scope and your thisis available inside it.
In plain javascript you could do something like this:
这将保留范围,并且您this可以在其中使用。在普通的 javascript 中,您可以执行以下操作:
var that = this;
data.forEach(function (eachObj) {
console.log("firms check!");
console.log(that.firms); // not working
a = a + eachObj.income;
eachObj.name = that.firms[data.firmid - 1].name; // wont work
});

