JavaScript-获取满足条件的数组元素 [英] JavaScript - get array element fulfilling a condition
问题描述
我正在使用W3C学习JavaScript,但找不到该问题的答案.
I'm learning JavaScript using W3C and I didn't find an answer to this question.
我正在尝试对满足某些条件的数组元素进行一些操作.
I'm trying to make some manipulations on array elements which fulfill some condition.
除了在for循环中的数组元素上运行之外,还有其他方法吗? 也许类似(用其他语言):
Is there a way to do it other than running on the array elements in for loop? Maybe something like (in other languages):
foreach (object t in tArray)
if (t follows some condition...) t++;
另一件事,有时我想使用元素的值,有时我想将其用作参考.句法上的区别是什么?
another thing, sometimes I want to use the element's value and sometimes I want to use it as a reference. what is the syntactical difference?
我也很高兴在更广泛的网站上获得推荐以学习JavaScript的建议. 谢谢
As well, I'll be happy for recommendations on more extensive sites to learn JavaScript from. thanks
推荐答案
In most browsers (not IE <= 8) arrays have a filter
method, which doesn't do quite what you want but does create you an array of elements of the original array that satisfy a certain condition:
function isGreaterThanFive(x) {
return x > 5;
}
[1, 10, 4, 6].filter(isGreaterThanFive); // Returns [10, 6]
Mozilla开发人员网络有很多很好的JavaScript资源.
Mozilla Developer Network has a lot of good JavaScript resources.
这篇关于JavaScript-获取满足条件的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!