简体   繁体   English

如何在数组中调用一定范围的数字?

[英]how can i call a range of numbers in an array?

So im wanting to compare a number(pullNumb)(gotten by another function elsewhere) to a range of numbers in an array. 所以我想比较一个数组中的数字范围内的数字(pullNumb)(由其他地方的另一个函数得到)。

for this example, lets say: 对于此示例,可以说:

var pullNumb = 100; 
var morn_T= new Array(6,7,8,9,10,11,12,13,14,15,16,17,18,19);

if(pullNumb>morn_T[1]){ document.write(" xyz ");

}

now instead of me writing out morn_T[1],[2],[3]; 现在不用我写出morn_T[1],[2],[3]; etc...is there a way to shorten this? 等...有没有办法缩短?

ideally if i could add this range to a variable that would be great. 理想情况下,如果我可以将此范围添加到一个很好的变量中。 but anything better than this long way ill gladly take. 但有什么比这漫长的旅程更好的选择了呢?

i found some page on here but...man that page was WAY to advanced for me....had other languages so i got lost. 我在这里找到了一些页面,但是...那个页面对我来说是高级的。...还有其他语言,所以我迷路了。

im still learning this so any tipslinks etc, i humble accept. 我仍然在学习这个,所以任何提示链接等,我谦虚地接受。

thanks in advance. 提前致谢。

Hum, so you wan't a loop? 哼,所以你不会循环吗?

var morn_T= new Array(6,7,8,9,10,11,12,13,14,15,16,17,18,19);
for(var i = 0; i < morn_T.length; i++)
    alert(morn_T[i]);

Demo (will spam alert ). 演示 (垃圾邮件alert )。

I would suggest you read up on JavaScript. 我建议您阅读JavaScript。 This is one of the best resource around. 这是最好的资源之一。

You could make a simple Range type like so: 您可以这样创建一个简单的Range类型:

var Range = function(a,b) {
  this.min = Math.min(a,b);
  this.max = Math.max(a,b);
}
Range.prototype.contains = function(x) {
  return ((this.min <= x) && (x <= this.max));
};
var oneThroughTen = new Range(1, 10);
oneThroughTen.contains(5); // => true
oneThroughTen.contains(0); // => false

You could add additional methods for inclusive/exclusiveness on the ends as well. 您也可以在末尾添加其他方法以包含/排除。

Of course, if your range is not continuous then you're best off looping through the contents of the array and comparing. 当然,如果范围不连续,那么最好循环遍历数组的内容并进行比较。

Is the array sorted at all? 数组是否已排序?
Are these document.writes in another sort of string array? 这些document.writes是否在另一种字符串数组中?

If the array happens to be sorted upwards, you could do the following: (Pseudo code, my javascript isn't the best, please modify as necessary) 如果该数组碰巧是向上排序的,则可以执行以下操作:(伪代码,我的javascript不是最好的,请根据需要进行修改)

var i = 0;
do
{
    if(PullNumb > morn_t[i])
    {   var stringVar = " "; 
        for(var j = 0; j <= morn_t[i]; j++)
        {
            stringVar += morn_t[j];
        }
        break;
    }
    i++; 
 }
 while( PullNumb < morn_t[i] && i < morn_t.Length());

So basically if the array is sorted, loop through it. 因此,基本上,如果数组已排序,则循环遍历。 Continue until the array is no more, or until the PullNumb is greater than that array value. 继续进行操作,直到不再存在该数组,或者直到PullNumb大于该数组值为止。

If the pullnumb is greater than that array value, start from the beginning of the array and append each corresponding string value to the stringVar variable. 如果pullnumb大于该数组的值,则从数组的开头开始,并将每个对应的字符串值附加到stringVar变量。

If, however, the array is just random numbers, and the strings (from document.write) aren't in any sort of array, I suppose using a switch statement is moderately faster. 但是,如果数组只是随机数,并且字符串(来自document.write)不在任何数组中,那么我想使用switch语句的速度要适中。

The concept you want may be Array.filter https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter It doesn't exist in every browser, but the page above explains how to implement it 您想要的概念可能是Array.filter https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter在每个浏览器中都不存在,但是上面的页面解释了如何实现它

The following code returns an array of all the numbers that are less than pullNumb 以下代码返回所有小于pullNumb的数字的数组

var biggerThan = morn_T.filter( function(a){ return pullNumb < a; } );
// Now you can just iterate through the filtered array
for (var i=0; i < biggerThan.length; i ++) { console.log( biggerThan[i] ); }

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

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