简体   繁体   English

“字符串”类型上不存在属性“名称”

[英]Property 'name' does not exist on type 'string'

I have a list of objects and I need to iterate over the list and add a certain property (string) from the object to a map of <string, boolean>.我有一个对象列表,我需要遍历列表并将某个属性(字符串)从 object 添加到 <string, boolean> 的 map。 Here is my code:这是我的代码:

this.selectedPeople.forEach(element => {
  this.checkedPeople.set(element.name, true);
}

The purpose of this is to set checkboxes to be checked for certain people (if they are in the selectedPeople list) using [checked] in Angular.这样做的目的是使用 Angular 中的[checked]设置要检查某些人的复选框(如果他们在 selectedPeople 列表中)。 For some reason, I get this error when trying to build the project:出于某种原因,我在尝试构建项目时收到此错误:

Property 'name' does not exist on type 'string'

I tried to change element.name to just element and that provides errors during compilation but when I look at the frontend, none of the checkboxes are checked (as you would expect since the key in the map is not a string as required).我尝试将element.name更改为仅element并在编译期间提供错误,但是当我查看前端时,没有检查任何复选框(正如您所期望的,因为 map 中的键不是所需的字符串)。 I have done similar things with other lists and they seem to be working fine, so why is this an issue here?我对其他列表做了类似的事情,它们似乎工作正常,那么为什么这是一个问题呢?

Edit1: I have seen some other solutions on here but they do not seem to be relevant to my case or have not worked. Edit1:我在这里看到了其他一些解决方案,但它们似乎与我的案例无关或没有奏效。

Edit2: This is not the actual code as it is company code so I have tried to recreate the issue using a different scenario so as not to run into any confidentiality issues. Edit2:这不是实际代码,因为它是公司代码,所以我尝试使用不同的场景重新创建问题,以免遇到任何保密问题。 To elaborate further, the selectedPeople array would be something like this:进一步阐述, selectedPeople 数组将是这样的:

[
  {
    "name":"Paul",
    "age":24,
    "sport":"Football"
  },
  {
    "name":"Tom",
    "age":22,
    "sport":"Tennis"
  }
]

Edit3: For further clarification, here is what my checkedPeople map looks like: Edit3:为了进一步澄清,这是我的checkedPeople map 的样子:

0: {"Paul" => false}
1: {"Jennifer" => false}
2: {"Georgia" => false}
3: {"Tom" => false}

And I am trying to change the value to true for each person who is in the selectedPeople array.我正在尝试将 selectedPeople 数组中的每个人的值更改为 true。

Without seeing your project setup, I'm betting that typescript thinks that element can be a string or an object... something like this:在没有看到您的项目设置的情况下,我敢打赌 typescript 认为该element可以是字符串或 object ... 像这样:

type SelectedPeople = Array<string | { name: string }>

In such as case, you will need to make sure the item is not a string before accessing the name property:在这种情况下,您需要在访问name属性之前确保该项目不是字符串:

this.selectedPeople.foreach(element => {
  if(typeof element === 'string') {
     this.checkedPeople.set(element, true);
  } else {
    this.checkedPeople.set(element.name, true);
  }
}

Yes, you can either try what Ryan is suggesting above, or alternatively you can add a safe check before setting each element like so:是的,您可以尝试 Ryan 上面的建议,或者您可以在设置每个元素之前添加安全检查,如下所示:

this.selectedPeople.forEach(element => {

If (element && element.Name){
  this.checkedPeople.set(element.name, true);
 }
}

I tried using element['name'] instead of element.name and that seemed to do the trick.我尝试使用element['name']而不是element.name ,这似乎可以解决问题。 I do not quite understand why this works (especially given that element.name is used elsewhere and works fine) so if anyone has any idea, feel free to comment.我不太明白为什么会这样(特别是考虑到element.name在其他地方使用并且工作正常),所以如果有人有任何想法,请随时发表评论。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM