如何从 javascript 中的关联数组中删除整个元素

how to delete an entire element from an associative array in javascript

我不太擅长 javascript.I 有一个关联数组,我在其中放置一些 values.Now 来遍历数组 我正在使用 foreach loop.Inside foreach 循环,如果满足条件,我想删除整个元素,并且 efficiency.However 的数组中没有空 space 我不知道如何从 foreach 循环中获取数组的索引。

here is my code:
for(var j = 0 ; j < i; j++){
        obstacles.push({ width:35, height:35, x:initialX, y:initialY});
    }

 //to iterate through the array
  obstacles.forEach(o => {
          o.x -= 2;
          context.fillRect(o.x, o.y, o.width, o.height); //create a rectangle with the elements inside the associative array
          //I need to get the index and delete the entire element and afterwards the array should resize with the other elements' index updated

        }

要从数组中移除符合特定条件的元素,Array.filter就派上用场了。参见 MDN

let arr1 = [1,2,3,4,5,6,7,8,9,10];
// [...] to stuff to arr1
arr1 = arr1.filter(val => val % 2 === 0);
console.log(arr1);