简体   繁体   English

未处理的拒绝(类型错误):无法读取未定义的属性“推送”

[英]Unhandled Rejection (TypeError): Cannot read property 'push' of undefined

i am getting this error, i just want to make a function that return all indexes of an array, but i dont know if i doing something wrong, it just shows an error than push can not be used, becouse undefined value.我收到此错误,我只想创建一个返回数组所有索引的函数,但我不知道我是否做错了什么,它只是显示一个错误,而不是 push 无法使用,因为值未定义。

here the code:这里的代码:

    function getIindex(array){
      let values
      for (let [ index, value ] of array.entries()) {
        values.push(index)
        console.log(values)
      }  
      return values
    }  

    let indexSubIndustry = getIindex(subRows)

You need to initialize the values variable with a value, empty array in your case.您需要使用values初始化values变量,在您的情况下为空数组。

function getIindex(array){
  let values = []; // here
  for (let [ index, value ] of array.entries()) {
    values.push(index)
    console.log(values)
  }  
  return values
}  

let indexSubIndustry = getIindex(subRows)

You might need to specify type hint for that array, depending on your TS configuration (eg with strict mode enabled).您可能需要为该数组指定类型提示,具体取决于您的 TS 配置(例如启用严格模式)。

let values: number[] = [];

Btw your code can be simplified a lot, if you want to return keys of the array parameter, just use keys() method:顺便说一句,您的代码可以简化很多,如果您想返回array参数的keys() ,只需使用keys()方法:

function getIindex(array){
  return [...array.keys()]; // convert iterator to array
}  

You should assign your values as an empty array instead of keeping it undefined.您应该将您的值分配为一个空数组,而不是保持未定义。


let values = [];

// Rest of the code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送” - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined 未处理的拒绝类型错误:无法读取未定义的属性“推送” - Unhandled rejection TypeError: Cannot read property 'push' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送”。 推送到另一个组件时遇到问题 - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined. Having issues pushing to another component 未处理的拒绝(TypeError):无法读取未定义的属性“子级” - Unhandled Rejection (TypeError): Cannot read property 'children' of undefined React-未处理的拒绝(TypeError):无法读取未定义的属性“ city” - React- Unhandled Rejection (TypeError): Cannot read property 'city' of undefined 未处理的拒绝(TypeError):无法读取未定义的 Stripe Javascript 的属性“id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined Stripe Javascript 反应未处理的拒绝(TypeError):无法读取未定义的属性“状态” - React Unhandled Rejection (TypeError): Cannot read property 'state' of undefined 用于 React 的 geolib - 未处理的拒绝(TypeError):无法读取未定义的属性“getCenter” - geolib for React - Unhandled Rejection (TypeError): Cannot read property 'getCenter' of undefined 未处理的拒绝(TypeError):无法读取未定义 MERN 的属性“长度” - Unhandled Rejection (TypeError): Cannot read property 'length' of undefined MERN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM