我尝试做delete weapons[i];,但是即使我做weapons.length时我仍然得到1。即使它应该为0。我如何确定如何从weapons[]数组中删除数组?

我这样做来梳理武器阵列:

        for (var i = 0, setsLen = weapons.length; i < setsLen; ++i ) {

             var searchWeapon = weapons[i].split("|");
             // console.log('['+i+'] >> Weapon ID: ' + searchWeapon[0] + ' | Y: ' + searchWeapon[1] + ' | X: ' + searchWeapon[2]);
             if (searchWeapon[1] == Y && searchWeapon[2] == X) {
                delete weapons[i];
             }
            }


然后将每个数组存储为3|10|4,其中3是武器ID,10是Y,4是X。

最佳答案

签出.splice方法

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice

使用您的示例:

for (var i = 0, setsLen = weapons.length; i < setsLen; ++i ) {
  var searchWeapon = weapons[i].split("|");
  if (searchWeapon[1] == Y && searchWeapon[2] == X) {
    weapons.splice(i,1);
  }
}

09-25 19:47