索引到数组中 [英] indices into an array
问题描述
原谅这位初学者的问题,但我似乎无法找到最好的方法来实现这一目标。
我有一个大的数组,我想找到其中的所有元素
介于两个值之间。例如:
var a = new Array();
a = [1,2,3,4,5,...,100];
并说我想找到a的所有元素。落在25到50之间。
Afor循环通过a的每个元素。只是好像没有效率。我的真实阵列比上面的例子大得多
(~10,000点)。有更好的方法吗?
谢谢,
Andy
Pardon the question from a beginner, but I can''t seem to find the best
way to do this.
I have a large array and I would like to find all elements within it
that fall between two values. For example:
var a = new Array();
a=[1,2,3,4,5,...,100];
And say I want to find all elements of "a" that fall between 25 and 50.
A "for" loop that goes through each element of "a" just seems
inefficient. My real array is much larger than the example above
(~10,000 points). Is there a better way to do this?
Thanks,
Andy
推荐答案
Andy在2006年5月18日晚上11:05发表以下文章:
Andy said the following on 5/18/2006 11:05 PM:
原谅一个初学者的问题,但我似乎无法找到最好的这样做的方法。
我有一个大型数组,我想找到其中的所有元素
,它们介于两个值之间。例如:
var a = new Array();
a = [1,2,3,4,5,...,100];
然后说我想要找到a的所有元素。落在25到50之间。
var begin = 25;
var end = 50;
for (var myVar = begin; myVar< end; myVar ++){
if(a [myVar]){
//用[myVar]做某事
}
}
Afor循环通过a的每个元素。只是看起来效率低下。我的真实阵列比上面的例子大得多(~10,000点)。有更好的方法吗?
Pardon the question from a beginner, but I can''t seem to find the best
way to do this.
I have a large array and I would like to find all elements within it
that fall between two values. For example:
var a = new Array();
a=[1,2,3,4,5,...,100];
And say I want to find all elements of "a" that fall between 25 and 50.
var begin = 25;
var end = 50;
for (var myVar = begin;myVar<end;myVar++){
if (a[myVar]){
//do something with a[myVar]
}
}
A "for" loop that goes through each element of "a" just seems
inefficient. My real array is much larger than the example above
(~10,000 points). Is there a better way to do this?
从开始到结束循环而不是循环整个数组。
- -
兰迪
comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周
Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /
Loop from begin to end instead of looping the entire array.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Andy在2006年5月18日下午11:05说:
Andy said the following on 5/18/2006 11:05 PM:
并说我想找到a的所有元素。落在25到50之间。
And say I want to find all elements of "a" that fall between 25 and 50.
Randy Webb写道:var begin = 25;
var end = 50;
for(var myVar =开始; myVar< end; myVar ++){
if(a [myVar]){
//用[myVar]做某事
}
}
Randy Webb wrote: var begin = 25;
var end = 50;
for (var myVar = begin;myVar<end;myVar++){
if (a[myVar]){
//do something with a[myVar]
}
}
哇...太棒了! (严重 - 没有讽刺)。
Wow... Splendid! (seriously - no sarcasm).
VK在2006年5月19日凌晨2:27发表以下声明:
VK said the following on 5/19/2006 2:27 AM:
Andy在2006年5月18日下午11:05说:
Andy said the following on 5/18/2006 11:05 PM:
并说我想找到a的所有元素。落在25到50之间。
And say I want to find all elements of "a" that fall between 25 and 50.
Randy Webb写道:
Randy Webb wrote:
var begin = 25;
var end = 50;
for(var myVar = begin; myVar< end; myVar ++){
if(a [myVar]){
//用[myVar]做某事
}
}
var begin = 25;
var end = 50;
for (var myVar = begin;myVar<end;myVar++){
if (a[myVar]){
//do something with a[myVar]
}
}
哇...太棒了! (严肃地说 - 没有讽刺)。
Wow... Splendid! (seriously - no sarcasm).
虽然它取决于之间的定义是什么虽然。
它包含或排除25和50?
排除它们:
var begin = 25; < br $>
var end = 50;
for(var myVar = begin + 1; myVar< end; myVar ++){
if( a [myVar])
{
//用[myVar]做某事
}
}
包括它们:
var begin = 25;
var end = 50;
for(var myVar = begin; myVar< = end; myVar ++){
if(a [myVar])
{
//用[myVar]做一些事情
}
}
JS很简单,当你''试着让它变得比它更难。
-
兰迪
comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周
Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /
Although it depends on what the definition of "between" is though. Does
it include or exclude 25 and 50?
Exclude them:
var begin = 25;
var end = 50;
for (var myVar = begin+1;myVar<end;myVar++){
if (a[myVar])
{
//do something with a[myVar]
}
}
Include them:
var begin = 25;
var end = 50;
for (var myVar = begin;myVar<=end;myVar++){
if (a[myVar])
{
//do something with a[myVar]
}
}
JS is simple when you don''t try to make it harder than it is.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
这篇关于索引到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!