Learn the JavaScript Array.every() and Array.some() methods

栏目: IT技术 · 发布时间: 4年前

内容简介:Since every item in the array is less than 10, the

Array.every() and Array.some() are handy JavaScript array methods that can help you test an array against specified criteria. In this post, we’ll quickly learn how to use them.

Array.every()

Array.every takes a callback function as an argument. If the function returns true for each item in the array, Array.every returns true. Let’s check it out.

function test(el) {
  return el < 10;
}

[1, 2, 3, 4, 5, 6].every(test);
// true

Since every item in the array is less than 10, the Array.every method returns true .

It’s common to pass an anonymous arrow function directly to the every method, so let’s do that to see a more familiar syntax:

[1, 2, 3, 4, 5, 6].every(el => el < 10);
// true

Let’s see another example. This time it’ll fail because not every element will pass the test.

[1, 2, 3, 4, 5, 6].every(el => el < 5);
// false

One nice thing that happens here is that, not only does it fail, but it exits early as soon as an element fails the test. That means it won’t even run the test on the last element of the array.

Array.some()

The Array.some method tests to see if at least one element of an array passes the test. This time, we’ll start with a failing example:

[1, 2, 3, 4, 5, 6].some(el => el > 10);
// false

Since none of the elements are greater than 10, Array.some returns false after testing each element.

Now a scenario that returns true :

[1, 2, 3, 4, 5, 6].some(el => el > 3);
// true

Not only does it return true , it returns true as soon as the first element passes the test . In this cases, since the number 4 passes the test, 5 and 6 aren’t even tested.

Other Functionality

Now that we generally know how the every and some methods work, here are some additional functionality you can get out of them:

  • The Array.every and Array.some can take a second argument that will be used as this in the execution of the callback function.
  • The callback function can take two additional arguments: the current item’s index and a reference to the array upon which the method was called.

Let’s cram all these additional features into an example:

const obj = { name: 'Daffodil' };

[1, 2, 3, 4, 5, 6].every(function(el, i, arr) {
  return el > i && arr[i] === el && this === obj;
}, obj);
// true

So why is this true?

  • Each element is greater than its index
  • arr[i] (where i is the current index` is identical to the current element)
  • Since we provided a reference to obj as the this argument, this is equal to obj

Conclusion

Hopefully you now have a couple additional array methods in your arsenal. Happy coding!


以上所述就是小编给大家介绍的《Learn the JavaScript Array.every() and Array.some() methods》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

剑指Offer

剑指Offer

何海涛 / 电子工业出版社 / 2012-1 / 45.00元

《剑指Offer:名企面试官精讲典型编程题》剖析了50个典型的程序员面试题,从基础知识、代码质量、解题思路、优化效率和综合能力五个方面系统整理了影响面试的5个要点。全书分为7章,主要包括面试的流程,讨论面试流程中每一环节需要注意的问题;面试需要的基础知识,从编程语言、数据结构及算法三方面总结了程序员面试的知识点;高质量的代码,讨论影响代码质量的3个要素(规范性、完整性和鲁棒性),强调高质量的代码除......一起来看看 《剑指Offer》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具