简体   繁体   English

TypeScript:数组中的拼接值抛出错误

[英]TypeScript: Splice Value from array throws Error

I want to remove a Value from an array.我想从数组中删除一个值。

My Code is runnign fine, but Visual Studio Code indicates that there is a Problem:我的代码运行良好,但 Visual Studio 代码表明存在问题:

My Code:我的代码:

 removeFromFav(){ var favoritesArray: Array<any> = this.getFavorites() for(let key in favoritesArray){ if(favoritesArray[key].hidx === this.hidx && favoritesArray[key].kontenrahmen === this.kontenrahmen ) { favoritesArray.splice(key, 1); } } localStorage.setItem( this.storageName, JSON.stringify(favoritesArray) ); }

Error Message:错误信息:

No overload matches this call.没有重载匹配此调用。 Overload 1 of 2, '(start: number, deleteCount?: number | undefined): any[]', gave the following error.重载 1 of 2, '(start: number, deleteCount?: number | undefined): any[]',给出以下错误。 Das Argument vom Typ "string" kann dem Parameter vom Typ "number" nicht zugewiesen werden. Das Argument vom Typ "string" kann dem 参数 vom Typ "number" nicht zugewiesen werden。 Overload 2 of 2, '(start: number, deleteCount: number, ...items: any[]): any[]', gave the following error.重载 2 of 2, '(start: number, deleteCount: number, ...items: any[]): any[]',给出了以下错误。 Das Argument vom Typ "string" kann dem Parameter vom Typ "number" nicht zugewiesen werden. Das Argument vom Typ "string" kann dem 参数 vom Typ "number" nicht zugewiesen werden。

The Problems seems to be the "key" in line 5 But how can I solve that?问题似乎是第 5 行中的“关键”但是我该如何解决呢?

Thanks in advance:)提前致谢:)

You are getting error because the syntax is not correct.您收到错误,因为语法不正确。 The following is syntax:以下是语法:

let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

You can mention the start index, delete count: number of items to deleted and item1..itemN: To be added in the array.您可以提及开始索引,删除计数:要删除的项目数和 item1..itemN:要添加到数组中。

You should specify the something like below:您应该指定如下内容:

favoritesArray.splice(parseInt(itemIndex), 1);

Since for..in returns key in string type and not number, you should parse to number and then use.由于 for..in 以字符串类型而不是数字返回键,因此您应该解析为数字然后使用。

For more details, please refer documentation here arraysplice有关更多详细信息,请参阅此处的文档arraysplice

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

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