jQuery inArray和Javascript IndexOf返回部分匹配。我需要检查完整的匹配

时间:2016-09-16 14:58:58

标签: javascript jquery arrays

我有一个包含对象的数组。我试图找出该数组中是否缺少ID。我正在使用的代码是返回部分匹配,例如允许“1”匹配“10”,“11”,“12”,“13”等等。如果我以字符串形式迭代或将搜索项和id转换为数字,则会发生这种情况。例如这段代码:

var loadUserTemplate = function(json){
    var firstArray = json[0];
    var parsed = (JSON.parse(firstArray));
    var gridStack = $('.grid-stack-item');
    var plength = parsed.length;

    gridStack.each(function(index){
        var gridstackId = Number($(this).attr('data-custom-id'));

        for(var j=0; j < plength; j++){
            var doesNotContain = Number(parsed[j].id.indexOf(gridstackId));
            console.log(typeof doesNotContain);
            if(doesNotContain === -1){
                console.log(gridstackId + " is not in " + parsed[j].id);
            }
        }
}

在console.log中生成这个(我只包括第一个数字,注意它在10 - 19中找到1):

number
1 is not in 2
number
1 is not in 3
number
1 is not in 4
number
1 is not in 5
number
1 is not in 6
number
1 is not in 8
number
1 is not in 9
number
1 is not in 20
number
1 is not in 22
number
1 is not in 23
number
1 is not in 24
number
1 is not in 25
number
1 is not in 26

我尝试过jQuery(inArray)和indexOf,结果相同。如何找到完整匹配,而不是部分匹配?

更新:解析firstArray解析为:

[{"x":"0","y":"0","width":"12","height":"5","id":"1"},{"x":"0","y":"5","width":"6","height":"2","id":"2"},{"x":"6","y":"5","width":"6","height":"2","id":"3"},{"x":"0","y":"7","width":"6","height":"2","id":"4"},{"x":"6","y":"7","width":"6","height":"2","id":"5"},{"x":"0","y":"9","width":"12","height":"8","id":"6"},{"x":"0","y":"17","width":"4","height":"6","id":"8"},{"x":"4","y":"17","width":"4","height":"6","id":"9"},{"x":"8","y":"17","width":"4","height":"6","id":"10"},{"x":"0","y":"23","width":"4","height":"6","id":"11"},{"x":"4","y":"23","width":"4","height":"6","id":"12"},{"x":"8","y":"23","width":"4","height":"6","id":"13"},{"x":"0","y":"29","width":"6","height":"8","id":"14"},{"x":"6","y":"29","width":"6","height":"8","id":"15"},{"x":"0","y":"37","width":"4","height":"6","id":"16"},{"x":"4","y":"37","width":"4","height":"6","id":"17"},{"x":"8","y":"51","width":"4","height":"6","id":"18"},{"x":"0","y":"43","width":"4","height":"6","id":"19"},{"x":"4","y":"51","width":"4","height":"6","id":"20"},{"x":"8","y":"57","width":"4","height":"6","id":"21"},{"x":"0","y":"57","width":"6","height":"8","id":"22"},{"x":"6","y":"63","width":"6","height":"8","id":"23"},{"x":"0","y":"65","width":"6","height":"8","id":"24"},{"x":"6","y":"71","width":"6","height":"8","id":"25"},{"x":"0","y":"79","width":"12","height":"10","id":"26"}]

gridstackId是DOM上元素的data-custom-id标记。

1 个答案:

答案 0 :(得分:2)

parsed[j].id的值是字符串,因此parsed[j].id.indexOf(gridstackId)会检查gridstackId的值是否包含 parsed[j].id 1}}(gridstackId的值也会在过程中转换为字符串。)

如果要检查相等,则应使用比较运算符:===

由于gridstackId是一个数字,您必须将parsed[j].id转换为数字:

Number(parsed[j].id) === gridstackId

或者,$(this).attr('data-custom-id')转换为数字,并将其保留为字符串:

var gridstackId = $(this).attr('data-custom-id');
// ...
parsed[j].id === gridstackId;

虽然要解决您的问题,即检查是否存在具有此ID的对象,您可以使用Array#some

var gridstackId = $(this).attr('data-custom-id');
var exists = parsed.some(function(obj) {
  return obj.id === gridstackId;
});
如果至少有一个对象满足条件(ID相等),则

exists将为true