Javascript 尝试在数组中查找值时出错

Javascript 尝试在数组中查找值时出错,javascript,node.js,arrays,typescript,Javascript,Node.js,Arrays,Typescript,我有一个数组rowSelected,它有两个元素。两个元素的任务类型值如下: const rowSelected = this.selection.selected; if (rowSelected.find(x => x.TaskType.includes('ABC'))) { this.myflag = true; } export interface TInterface { objectId:

我有一个数组
rowSelected
,它有两个
元素。两个
元素
任务类型
值如下:

const rowSelected = this.selection.selected;
            if (rowSelected.find(x => x.TaskType.includes('ABC'))) {
                this.myflag = true;
            }
export interface TInterface {
    objectId: string;
    hostName: string;
}
第一个元素具有
TaskType:first

第二个元素具有
TaskType:second

现在,如果任何元素具有
TaskType:First
,那么我想将布尔变量
this.myflag
设置为
true

我正在使用
数组。查找
如下:

const rowSelected = this.selection.selected;
            if (rowSelected.find(x => x.TaskType.includes('ABC'))) {
                this.myflag = true;
            }
export interface TInterface {
    objectId: string;
    hostName: string;
}
上面的代码给出了一个错误
类型“TInterface”上不存在属性“TaskType”。

但是,当我执行
rowSelected.find(x=>console.log(x.TaskType))
时,我能够打印
类型

console.log(rowSelected)
正在返回

0: {hostName: "ABC.COM",TaskType: "First", …}
length: 1
__proto__: Array(0)
TInterface
如下所示:

const rowSelected = this.selection.selected;
            if (rowSelected.find(x => x.TaskType.includes('ABC'))) {
                this.myflag = true;
            }
export interface TInterface {
    objectId: string;
    hostName: string;
}

我不知道为什么它会抱怨
TInterface
,因为它与
rowSelected

毫无关系。我认为您需要使用
instanceof
而不是
。type
typeof

myflag = false
class First {
    name: string
}
const first: First = new First(name: "first")
class Second {
    name: string
}
const second: Second = new Second(name: "second")
let rowSelected = [
    first,
  second
]
rowSelected.forEach(x => { 
    if( x instanceof First ) {
        myflag = true;
    }
})
console.log(myflag)

它可能不会在您的类型中捕获。你能显示
rowSelected
TInterface
的类型吗
typeof
正在为
rowSelected
返回
object
如何处理它吗?发布数据集的子集和
TInterface
你可以尝试用
x.TaskType.includes('ABC')
替换
x.TaskType.includes('ABC')
,或者,如果这不起作用,请张贴TaskType界面的外观。看起来您尝试了上述方法,但没有起作用。正如@PritamKadam和@mwilson在上面发布的一样,在代码中找到
TInterface
,然后发布其中的内容。这看起来不正确。这里的
type
不是类类型。这是对象xIn的一个属性,在这种情况下,我认为rowSelected中每个元素的类型都有问题,因为代码工作得很好。您能发布
rowSelected.forEach(x=>console.log(x))
的结果吗?我已经更新了我的问题。
console.log(rowSelected)
返回0:{hostName:“ABC.COM”,TaskType:“ABC”,…}长度:1 proto:Array(0)