Searching through arrays has traditionally involved writing an exhaustive loop that inspected each element in the array, checking if they matched a particular value. Thankfully, modern JavaScript provides two solutions that are far more efficient: one new in JS 2015, and one a little older.
在数组中进行搜索通常涉及编写一个穷举循环 ,该穷举循环检查数组中的每个元素,检查它们是否与特定值匹配。 幸运的是,现代JavaScript提供了两种效率更高的解决方案:一种是JS 2015中的新解决方案,另一种是旧的。
包括 (includes)
We’ll start with the most recent: includes
. Very simply, includes
returns a value of true
or false
if an array contains a particular value. Given an array of the following elements:
我们将从最新的includes
开始: includes
。 很简单,如果数组包含特定值&