一个简单的检查如果选中,如果没有添加到Javascript中的数组

一个简单的检查如果选中,如果没有添加到Javascript中的数组,javascript,arrays,Javascript,Arrays,此函数用于获取所选行的id并检查它是否在数组中,如果它不在数组中,则将其添加到数组中,否则将其从数组中删除。问题是我似乎无法正确地安排事件的顺序。由于中断,循环不会一直运行,但是如果在图像更改复选框工作时删除中断,数组仍然是错误的 我也不明白为什么尽管声明deleteString=[];在该函数外部,不将其放入函数内部,调用deleteString.pushorderId;失败 问题似乎很明显,在第一次运行时,无论数组有多大,无论检查是否匹配,循环的其余部分都不会运行。因此,在使用find/no

此函数用于获取所选行的id并检查它是否在数组中,如果它不在数组中,则将其添加到数组中,否则将其从数组中删除。问题是我似乎无法正确地安排事件的顺序。由于中断,循环不会一直运行,但是如果在图像更改复选框工作时删除中断,数组仍然是错误的

我也不明白为什么尽管声明deleteString=[];在该函数外部,不将其放入函数内部,调用deleteString.pushorderId;失败

问题似乎很明显,在第一次运行时,无论数组有多大,无论检查是否匹配,循环的其余部分都不会运行。因此,在使用find/not found的结果之前,也许我应该进行一次检查,等待循环完成

function passSelection(orderId) {
  // check if empty
  if (deleteString.length == 0) {
    // turn into array
    deleteString = [];
    // first entry
    deleteString.push(orderId);
    // mark this row as checked
    $("#"+"select-box-"+orderId).attr('src', 'images/red-checked.png');
  }
  else {
    // not the first order
    // check if already in array
    // get length of array
    var delStrLen = deleteString.length;
    // loop through array
    for (var i = 0; i < delStrLen; i++) {
      if (deleteString[i] == orderId) {
        // match found, remove from deleteString array
        deleteString.splice(i, 1);
        // update the row
        $("#"+"select-box-"+orderId).attr('src', 'images/unchecked.png');
        break;
      }
      else {
        // not in the array
        // add to array
        deleteString.push(orderId);
        // update row
        $("#"+"select-box-"+orderId).attr('src', 'images/red-checked.png');
        break;
      }
    }
  }
}

首先,删除for循环中的元素不是一个好主意,因为您正在操作要遍历的数组。要查找数组中的项,应使用indexOf方法


您的问题是在循环中添加元素。您应该在循环中检查是否存在,然后添加/删除项目一次。您也不需要对空数组进行特殊检查,您的循环将不会启动,并且将按预期工作:

function passSelection(orderId) {
    var isFound = false;

    for (var i = 0; i < deleteString.length; i++) {
        if (deleteString[i] == orderId) {
            isFound = true;
        }
    }

    if (isFound) {
        deleteString.splice(i, 1);
        $("#" + "select-box-" + orderId).attr('src', 'images/unchecked.png');
    } else {
        deleteString.push(orderId);
        $("#" + "select-box-" + orderId).attr('src', 'images/red-checked.png');
    }
}
如果您需要ECMAScript 6之前的浏览器支持,那么您可以采用相同的方式利用对象属性:

var set = {};
function passSelection(orderId) {
    if (set[orderId]) {
        $("#"+"select-box-"+orderId).attr('src', 'images/unchecked.png');
        delete set[orderId];
    } else { 
        $("#"+"select-box-"+orderId).attr('src', 'images/checked.png');
        set[orderId] = true;
    }
}

您应该使用indexOf来测试数组中元素的存在性。 indexOf返回搜索项的位置,如果找不到元素,则返回-1 这是我重构和测试过的代码

var deleteString = [];
function passSelection(orderId) {
  // check if empty
  let orderPos = deleteString.indexOf(orderId);
  if (orderPos == -1) {
    // first entry
    deleteString.push(orderId);
    // mark this row as checked
    $("#"+"select-box-"+orderId).attr('src', 'images/red-checked.png');
  }
  else {

    // match found, remove from deleteString array
    deleteString.splice(orderPos, 1);
    // update the row
    $("#"+"select-box-"+orderId).attr('src', 'images/unchecked.png');
  }
}

实际上,您不需要第一个条件来检查数组长度是否为零。并从其他条件中移除中断。根据建议,使用indexOf方法。而不是循环。另外,请尝试发布一个完整的JSFIDLE示例,以便每个人都能轻松理解您的逻辑。请查看修改后的代码

function passSelection(orderId) {
  if (!deleteString) deleteString = [];

  var index = deleteString.indexOf(orderId);
  if (index !== -1) {
    deleteString.splice(index, 1);
    $("#"+"select-box-"+orderId).attr('src', 'images/unchecked.png');
  } else {
    deleteString.push(orderId);
    $("#"+"select-box-"+orderId).attr('src', 'images/red-checked.png');
}

初始化数组以避免检查它是否存在

var arr = [ 4,5,6,1,23 ];
function remove(orderId) {
        var src;
        var index;
        index = arr.indexOf(orderId);
        if (index === -1) {
                arr.push(orderId);
                src = 'images/unchecked.png';
        } else {
                arr.splice(1,index);
                src = 'images/red-checked.png';
        }
        $("#"+"select-box-"+orderId).attr("src",src);
}
// Test item which is in the array (it should be removed)
remove(5);
console.log(arr);
// Test item which is not in the array (it should be added)
remove(24);
console.log(arr);

您可以使用indexOf来查找元素是否在数组中,而不是循环所有元素。@dfasoro好的,我将尝试一下,我正在查看MDN的数组页面,我已经看到了,但没有想到使用它。Thanks@joehungjohn请检查我的方法。问题是,这种方法的速度慢了N倍,算法性能差。感谢您使用对象的替代方法和更干净的方法。很难选择答案。我很感谢你的时间和新方法。一般来说,对于javascript/编码,我不知道什么时候使用对象。@joehungjohn这不是关于将答案标记为正确。这是关于注意不同的方法,并找到最合适的方法:了解速度是很好的,在这种情况下,集合可能不会大于10个集合的两个集合,但我明白你的意思,将选择此解决方案,特别是因为代码要短得多。实际上,我会发布控制台输出,但在切换到i3wm之后,我的键绑定消失了,哈哈,所以无法触发屏幕截图功能。下次关于JSFIDLE,我会记住这一点。
function passSelection(orderId) {
  if (!deleteString) deleteString = [];

  var index = deleteString.indexOf(orderId);
  if (index !== -1) {
    deleteString.splice(index, 1);
    $("#"+"select-box-"+orderId).attr('src', 'images/unchecked.png');
  } else {
    deleteString.push(orderId);
    $("#"+"select-box-"+orderId).attr('src', 'images/red-checked.png');
}
var arr = [ 4,5,6,1,23 ];
function remove(orderId) {
        var src;
        var index;
        index = arr.indexOf(orderId);
        if (index === -1) {
                arr.push(orderId);
                src = 'images/unchecked.png';
        } else {
                arr.splice(1,index);
                src = 'images/red-checked.png';
        }
        $("#"+"select-box-"+orderId).attr("src",src);
}
// Test item which is in the array (it should be removed)
remove(5);
console.log(arr);
// Test item which is not in the array (it should be added)
remove(24);
console.log(arr);