简体   繁体   English

javascript数组和/或jquery $ .inArray()和.splice()

[英]javascript arrays and/or jquery $.inArray() and .splice()

I am working with an array that can have only 12 values in it; 我正在使用只能有12个值的数组; 1 through 12. These values can exist in any order. 1到12。这些值可以任何顺序存在。 I need to go through the array and find the first unused value and then set it to a variable. 我需要遍历数组,找到第一个未使用的值,然后将其设置为变量。

For example, if the array contains the values [1, 7, 2, 5, 10] I want to set the variable to 3 since it was the first unused number in numerical order. 例如,如果数组包含值[1、7、2、5、5、10],我想将变量设置为3,因为它是按数字顺序的第一个未使用的数字。 I am working with jQuery 1.9.1 and jQuery UI 1.10.1, but if this can be solved in plain javascript that is fine too! 我正在使用jQuery 1.9.1和jQuery UI 1.10.1,但是如果可以用普通的javascript解决,那也很好!

Below is my code, which I know is wrong since it always sets the variable to 1. Note: tCounter is a global array used in several functions, so I do not want to modify the contents here. 下面是我的代码,我知道这是错误的,因为它始终将变量设置为1。注意:tCounter是几个函数中使用的全局数组,因此我不想在此处修改内容。 This is why I created tArray to splice values out of. 这就是为什么我创建tArray来拼接值的原因。 Thanks for your help! 谢谢你的帮助!

var i;
var t;
var tArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

for (i = 1; i < 12; i++) {
    if (jQuery.inArray(i, tCounter)) {
        tArray.splice($.inArray(i, tArray), 1);
    }
}

if (tArray.length <= 0) {
    term = 1;
} else {
    term = tArray[0];
}

There are two errors that I can see in your current implementation. 我可以在您当前的实现中看到两个错误。

First, your for loop indexing appears to be one off. 首先,您的for循环索引似乎是一次性的。 In its current state, it will not check for the existence of the number 12, (due to your conditional i<12 ). 在当前状态下,由于条件i<12 ,它不会检查数字12是否存在。

Second, your if statement actually evaluates to false when it should be true. 其次, if语句应为true时,其实际值为false。 If the index in the array is 0, (0 evaluates to false in Javascript), you should enter the statement. 如果数组中的索引为0(在Javascript中0等于false),则应输入该语句。 Taking what you have I would rewrite your code as follows: 考虑到您拥有的内容,我将按照以下方式重写您的代码:

  var i;
  var t; //this isn't used, perhaps it should be 'term'?

  for (i = 1; i < 13; i++) {
     // if we find a number that isn't in tCounter, we exit the loop.
     if (jQuery.inArray(i, tCounter) === -1) {
         term = i;
         break;
     }

     //sets term to null if all numbers are present in array.
     term = null;
  }

If your values were not the numbers 1-12 but rather arbitrary numbers, your idea of splicing values out of an array would work nicely, but as is, you really don't need the var tArray at all. 如果您的值不是数字1-12,而是任意数字,那么将值拼接出数组的想法会很好地工作,但实际上,您根本不需要var tArray。

Sidenote: In an interesting comparison, it seems that if you are looking for the fastest implementation possible, you should use tCounter.indexOf(i) instead of jQuery.inArray(i, tCounter) , I wasn't sure which was faster, but this nifty test confirmed my suspicions. 旁注:在一个有趣的比较中,似乎如果您正在寻找最快的实现方式,则应该使用tCounter.indexOf(i)而不是jQuery.inArray(i, tCounter) ,我不确定哪个会更快,但是这个漂亮的测试证实了我的怀疑。

A naive solution is to iterate from 1-12 and check for the existence of that value in the array. 一个幼稚的解决方案是从1-12进行迭代,并检查数组中该值的存在。 Like this: 像这样:

var i;
for(i=1; i<12; i+=1) {
   if (tArray.indexOf(i) === -1) break;
}

term = i;

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

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