2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7dab367417b2b2512b6f
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 使用 some 方法检查数组中是否有元素是否符合条件
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 1
|
2020-08-05 16:38:04 +08:00
|
|
|
|
forumTopicId: 301314
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
`some`方法用于检测数组中*任何*元素是否满足指定条件。如果有一个元素满足条件,返回布尔值`true`,反之返回`false`。
|
|
|
|
|
|
|
|
|
|
举个例子,下面的代码检测数组`numbers`中是否有元素小于10:
|
2020-08-05 16:38:04 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var numbers = [10, 50, 8, 220, 110, 11];
|
|
|
|
|
numbers.some(function(currentValue) {
|
|
|
|
|
return currentValue < 10;
|
|
|
|
|
});
|
|
|
|
|
// Returns true
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
在`checkPositive`函数值中使用`some`检查`arr`中是否有元素为正数,函数应返回一个布尔值。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
应该使用`some`method.
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.some/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`checkPositive([1, 2, 3, -4, 5])`应返回`true`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(checkPositive([1, 2, 3, -4, 5]));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`checkPositive([1, 2, 3, 4, 5])`应返回`true`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(checkPositive([1, 2, 3, 4, 5]));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`checkPositive([-1, -2, -3, -4, -5])`应返回`false`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(!checkPositive([-1, -2, -3, -4, -5]));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-05 16:38:04 +08:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|