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)