【发布时间】:2026-01-29 01:30:02
【问题描述】:
我写了代码:
for (var game in this.currentGames) {
if (this.currentGames[game].gameState === consts.GS_GAME_DELETE) {
delete this.currentGames[game];
}
}
它工作正常,但 JSLint.net 向我显示警告:
JSLint:意外的“变量”。
当我试图修复它时:
var game;
for (game in this.currentGames) {
if (this.currentGames[game].gameState === consts.GS_GAME_DELETE) {
delete this.currentGames[game];
}
}
我收到这样的警告: JSLint:预期为“Object.keys”,但看到“for in”。
我尝试修复并编写下一个代码:
Object.keys(this.currentGames).forEach(function (item, i, arr) {
if (arr[i].gameState === consts.GS_GAME_DELETE) {
delete arr[i];
}
});
它只是不起作用,因为 arr 是数组(不是关联数组)。
当我尝试:
Object.keys(this.currentGames).forEach(function (item) {
if (this.currentGames[item].gameState === consts.GS_GAME_DELETE) {
delete this.currentGames[item];
}
});
我得到运行时错误:
if (this.currentGames[item].gameState === consts.GS_GAME_DELETE) {
^
TypeError: 无法读取未定义的属性“currentGames”
我使用 Microsoft Visual Studio 2012 和 JSLint.NET。
所以,我的问题是:在循环中从关联数组中删除元素的正确方法是什么?或者在哪里可以关闭 JSLint?
【问题讨论】:
标签: javascript node.js jslint