JavaScript - 获取满足条件的数组元素 [英] JavaScript - get array element fulfilling a condition

查看:80
本文介绍了JavaScript - 获取满足条件的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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

推荐答案

在大多数浏览器(不是 IE <= 8)中,数组都有一个 filter 方法,该方法并不能满足您的要求,但会为您创建原始数组的元素数组满足一定条件:

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 Developer Network 拥有大量优秀的 JavaScript 资源.

Mozilla Developer Network has a lot of good JavaScript resources.

这篇关于JavaScript - 获取满足条件的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆