简体   繁体   English

为什么我在此 for 循环中无法读取未定义的属性“startsWith”?

[英]Why am I getting Cannot read property 'startsWith' of undefined in this for loop?

I am trying to sort an array by 'version' and then identify all string that begin with 'iPad'.我正在尝试按“版本”对数组进行排序,然后识别以“iPad”开头的所有字符串。

The following code does not log anything and returns an error.以下代码不记录任何内容并返回错误。

orderedUsers: function () {
  let newarray = sortBy(this.jobs, 'version').reverse()
  for (let i in newarray) {
    if (i.version.startsWith('iPad')) {
      console.log(i.version);
    }
  }
  return newarray

error:错误:

TypeError: Cannot read property 'startsWith' of undefined

If I remove the for-loop and just put:如果我删除 for 循环并放置:

orderedUsers: function () {
  let newarray = sortBy(this.jobs, 'version').reverse()
  return newarray

The list is correctly sorted by version.该列表按版本正确排序。 This makes me think the error is related to how I have written my for-loop or if statement.这让我认为错误与我如何编写 for 循环或 if 语句有关。

What am I doing wrong here.我在这里做错了什么。

The for...in construct is not doing what you think here. for...in构造并没有按照您的想法进行操作。 It is is designed to iterate over objects.它旨在迭代对象。 So in this case it is treating newArray as an object but with the property names as the array indices.所以在这种情况下,它将newArray视为一个对象,但将属性名称作为数组索引。 Arrays in Javascript are just objects with numeric property names. Javascript 中的数组只是具有数字属性名称的对象。 More specifically if you changed your code to:更具体地说,如果您将代码更改为:

for (let i in newarray) {
    if (i.version.startsWith('iPad')) {
      console.log(i);
    }
  }

You would clearly see the problem.你会清楚地看到问题所在。 i is a number and not a job object. i是一个数字而不是一个工作对象。

fix:使固定:

orderedUsers: function () {
  let newarray = sortBy(this.jobs, 'version').reverse()
  for (let i in newarray) {
    if (newarray[i].version.startsWith('iPad')) {
      console.log(newarray[i].version);
    }
  }
  return newarray

暂无
暂无

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

相关问题 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么会出现“无法读取未定义的html属性”? - Why am I getting “Cannot read property of html undefined”? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 为什么在此示例中出现“无法读取未定义的属性'文本'”? - Why am I getting “Cannot read property 'text' of undefined” in this example? 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 为什么我得到这个“无法读取未定义的属性”长度”? (JavaScript) - Why am I getting this 'cannot read property 'length' of undefined'? (JavaScript) 无法读取未定义的属性“startsWith” - Cannot read property 'startsWith' of undefined 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”? - why am I getting Uncaught TypeError: Cannot read property '0' of undefined on assign?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM