JavaScript——删除指定数组元素

// 删除指定数组元素
    var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
    for (var i = arr.length - 1; i >= 0; i--) {
      if (arr[i] == 0) {
        while (i < arr.length) {
          arr[i] = arr[i + 1];
          i++;
        }
      }
    }
    console.log(arr);

    // 方法二:
    var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
    var newArry = [];
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] = 0) {
        newArry[newArry.length] = arr[i];
      }
    }
    console.log(newArry);

你可能感兴趣的:(JavaScript学习笔记,javascript,es6,前端)