简体   繁体   English

如何检查Object中是否存在数字数组

[英]How to check if array of numbers exist in Object

I am trying to write some code that will check to see if a group of numbers stored in an array exist in an object. 我正在尝试编写一些代码,用于检查存储在数组中的一组数字是否存在于对象中。 With the code I have now it is always returning -1. 使用我现在的代码,它总是返回-1。

function checkHorizonal() {
    var x= ['1', '2' ,'3'];
    var y= ['4', '5', '6'];
    var z= ['7', '8', '9']


        console.log(jQuery.inArray(x, squaresClicked));

}

This is the squaresClicked object contents: 这是squaresClicked对象的内容:

Object {1: "1", 2: "-1", 3: "1"}

You can see that the keys 1,2,3 exist but it will return -1. 您可以看到键1,2,3存在,但它将返回-1。

jQuery's inArray can't deal with an array of needles, otherwise jQuery的inArray不能处理一系列针,否则

$.inArray(x, Object.keys(squaresClicked));

would've worked. 会工作的。 In this case you are stuck with iterating, I guess (though there might be some other tricky way – as always). 在这种情况下,你会陷入迭代,我想(尽管可能还有其他一些棘手的方法 - 一如既往)。 One way in jQuery (though I don't very much like it for various reasons 1 ): jQuery的一种方式(虽然我不太喜欢它因各种原因1 ):

var hasFailed = false;
$.each(x, function (index, value) {
    if($.inArray(value, Object.keys(squaresClicked)) === -1) {
        hasFailed = true;
    }
});

return !hasFailed;

A basic vanillaJS approach: 基本的vanillaJS方法:

for(var i = 0; i < x.length; i++) {
    if(!squaresClicked.hasOwnProperty(x[i])) {
        return false;
    }
}

return true;

1 Two of the reasons why I dislike it: 1我不喜欢它的两个原因:

  • It will iterate over the entire array, even if it's already clear that it will fail 它会迭代整个数组,即使它已经很清楚它会失败
  • It uses a double negation in its logic 它在逻辑上使用了双重否定

From the jQuery doc: 来自jQuery doc:

jQuery.inArray( value, array [, fromIndex ] )

jQuery.inArray won't work the way you're using it. jQuery.inArray不会像你使用它一样工作。 It checks for array contents, not object keys. 它检查数组内容,而不是对象键。 Your value x is fine, but you're passing in an object instead of an array. 您的值x很好,但是您传入的是对象而不是数组。

If you're trying to see if a set of integers exists as keys in an object, you can try the following (assuming you're using JavaScript >= 1.6): 如果您正在尝试查看一组整数是否作为对象中的键存在,您可以尝试以下方法(假设您使用的是JavaScript> = 1.6):

myArray.filter(function(x) {
  // check if value is a key
  return (x in squaresClicked);
}).length == myArray.length;

Basically, we iterate through our array and return only those that exist as keys in the object squaresClicked . 基本上,我们遍历数组并仅返回在对象squaresClicked作为键存在的squaresClicked If the new array has the same length as our original list, then the values must all exist as properties in the object. 如果新数组的长度与原始列表的长度相同,则值必须全部作为对象中的属性存在。

If you don't want to look up an objects entire prototype chain for the array value, you'll need to use hasOwnProperty instead of in . 如果您不想为数组值查找对象的整个原型链,则需要使用hasOwnProperty而不是in

Like so: 像这样:

return (squaresClicked.hasOwnProperty(x));

You can read more about this method here . 您可以在此处详细了解此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM