Javascript 无法读取find函数中未定义的属性

Javascript 无法读取find函数中未定义的属性,javascript,angular,Javascript,Angular,如果存在值,我将使用javascripts.find()函数返回一个对象,但是我得到一个未定义的错误 this.reduceoptionRadio = 'instalment' this.calculatorResult = [ { "newCalculatedInstallmentsNo": "50", "newContractEndDate": "20250701",

如果存在值,我将使用javascripts
.find()
函数返回一个对象,但是我得到一个未定义的错误

this.reduceoptionRadio = 'instalment'
this.calculatorResult = 
[
    {
        "newCalculatedInstallmentsNo": "50",
        "newContractEndDate": "20250701",
        "newResidual": "0",
        "newInstalment": "4071.91",
        "newTerm": "52",
        "outBalanceAvaf": null,
        "restructureType": "term"
    },
    {
        "newCalculatedInstallmentsNo": "52",
        "newContractEndDate": "20250901",
        "newResidual": "0",
        "newInstalment": "3939.93",
        "newTerm": "54",
        "outBalanceAvaf": null,
        "restructureType": "instalment"
    }
]



this.calculateFormData = this.calculatorResult.find(
  function(el) {
  return el.restructureType === this.reduceoptionRadio;
  }
);
console.log(this.calculateFormData);
如何获取this.reduceoptionRadio的值以使用find函数反映?
this.reduceoptionRadio
的值是动态的,我只是在这里硬编码,以便有相关的代码来解释


控制台中的确切错误读取错误类型错误:无法读取未定义的属性“ReduceOptions Radio”

函数的ES5语法
函数(el)
创建新上下文(新的
)。在它里面,
这个
指的是函数本身,其中
reduceoptionRadio
在逻辑上是未定义的

一个旧的(ES6之前)肮脏的解决方法过去是:

var self = this;

this.calculateFormData = this.calculatorResult.find(
  function(el) {
      return el.restructureType === self.reduceoptionRadio;
  }
);
相反,请使用较新的ES6“fat arrow”语法,该语法在函数中传递上下文(
this
):

this.calculateFormData = this.calculatorResult.find( el => el.restructureType === this.reduceoptionRadio );

在“查找”的内部,尝试使用箭头函数。它将解决问题。当我尝试ES6版本时,如果我使用console.log It
this.calculateFormData
this.calculateFormData=this.calculatorResult.find(el=>el.restructionType==this.reduceoptionRadio),我会将
this.calculateFormData
设置为未定义
console.log(this.calculateFormData)@skydev不可复制:。数组是异步填充的吗?@adiga是异步填充的。它适用于平面数据,但不适用于平面数据asynchronously@skydev填充数组后,您需要执行
this.calculatorResult.find
。当然,如果您试图在尚未填充的数组中查找内容,您将找不到任何内容:)