実行環境 Node.js 15.2.1

公開日 2021年2月14日 JST

コード例の出力としてconsole.logを使用しています。

使用方法がわからない場合は以下のページを参照してからコード例を確認してください。

console.logの使用方法

some(Array、配列)

このページでは豊富な例を用いてJavaScript(js)のArrayオブジェクトのsomeメソッドの使い方を学ぶことができます。

JavaScript(js)のsomeメソッドはArrayオブジェクト(配列)のメソッドの1つです。

someメソッドは呼び出し元の配列の要素に対して、第1引数で指定する関数(コールバック関数)を順番に実行します。
返り値は真偽値(boolean)で、コールバック関数の返り値がtrueとなる要素が1つでもある場合はsomeメソッドはtrueを返し、それ以外はfalseを返します。

空の配列に対しては常にfalseを返すので注意してください。

TL;DR

基本

// 配列の要素の値に対して、第1引数で指定した関数(コールバック関数)が順番に実行される

// 要素の値のどれか1つでもコールバック関数でtrueを返す場合
const array1 = [100, 200, 300];
const callback1 = function (value) {
  return value >= 300;
};
const returnValue1 = array1.some(callback1);
console.log(returnValue1);
==> true
// 呼び出し元の配列には影響なし
console.log(array1);
==> [ 100, 200, 300 ]

// 要素の値のどれもコールバック関数でtrueを返さない場合
const array2 = [100, 200, 300];
const callback2 = function (value) {
  return value > 1000;
};
console.log(array2.some(callback2));
==> false






// someメソッドはコールバック関数からtrueが返ってきた時点でループ処理を中断する
const array3 = ['js', 'javascript', 'typescript'];
const returnValue3 = array3.some((value) => {
  console.log(value);
  return value.length >= 5;
});
==> js
==> javascript
console.log(returnValue3);
==> true





// 空配列の場合は常にfalse
const array4 = [];
const returnValue4 = array4.some(value => true);
console.log(returnValue4);
==> false





// コールバック関数で「真(true)と評価される値」が返ってくればsomeはtrueを返す
const array5 = ['A', 'B', 'C'];
const returnValue5 = array5.some((value) => {
  return value;
});
console.log(returnValue5);
==> true





const array6 = ['A', 'B', 'C'];
const returnValue6 = array6.some(() => 0);
console.log(returnValue6);
==> false

要素のインデックス取得

// コールバック関数の第2引数で、処理している要素のインデックスを取得できる
const array1 = ['js', 'JavaScript', 'TypeScript'];
const returnValue1 = array1.some((value, index) => {
  console.log(index);
  return value === index;
});
==> 0
==> 1
==> 2
console.log(returnValue1);
==> false





const array2 = [5, 1, 'js'];
const returnValue2 = array2.some(function (value, index) {
  console.log(index);
  return typeof value === 'number' && index === value;
});
==> 0
==> 1
console.log(returnValue2);
==> true

関連情報:typeof演算子の使用方法

呼び出し元配列の取得

// コールバック関数の第3引数を指定して呼び出し元の配列を取得できる
const array1 = ['Foo', 'Bar'];
const returnValue1 = array1.some((value, index, array) => {
  console.log(array, array[index]);
});
==> [ 'Foo', 'Bar' ] Foo
==> [ 'Foo', 'Bar' ] Bar
console.log(returnValue1);
==> false

コールバック関数内でのthis指定

function Student(name, favoriteSubject) {
  this.name = name;
  this.favoriteSubject = favoriteSubject;
}

Student.prototype.includesFavoriteSubject = function (subjects) {
  return subjects.some(value => {
    return value === this.favoriteSubject;
  }, this);
};

const subjects = ['reading', 'writing', 'math'];
const student1 = new Student('Josh', 'math');
const student2 = new Student('Ruoxi', 'science');
const returnValue1 = student1.includesFavoriteSubject(subjects);
console.log(returnValue1);
==> true
const returnValue2 = student2.includesFavoriteSubject(subjects);
console.log(returnValue2);
==> false

解説

基本

someメソッドは呼び出した配列の各要素に対し、第1引数で指定した関数(コールバック関数)を実行します。 コールバック関数の返り値がtrueとなる要素が1つでもある場合はsomeメソッドはtrueを、それ以外の場合はfalseを返します。
例外として、空配列に対してsomeメソッドは実行した場合は常にfalseを返します。

「コールバック関数の第1引数」を指定すると、処理している配列の「要素の値」を取得することができます。

注意点として、someメソッドが配列の要素に対してコールバック関数を呼び出すのは、 「コールバック関数の返り値が真(true)となるまで」であることが挙げられます。 呼び出し元の配列のすべての要素に対してコールバック関数が呼び出されるわけではないことに気をつけてください。

// 配列の要素の値が1つでもコールバック関数でtrueを返す場合
const array1 = ['Foo', 'Bar', 'Ninja'];
const callback1 = (value) => {
  return value.length === 5;
};
const returnValue1 = array1.some(callback1);
console.log(returnValue1);
==> true
// 呼び出し元配列には影響なし
console.log(array1);
==> [ 'Foo', 'Bar', 'Ninja' ]

// 配列の要素の値がすべてコールバック関数でfalseを返す場合
const array2 = ['Foo', 'Bar', 'Ninja'];
const returnValue2 = array2.some(function (value) {
  return value.length >= 10;
});
console.log(returnValue2);
==> false






// 配列の途中でコールバック関数がtrueを返す場合
const array3 = [1, 2, 3];
const returnValue3 = array3.some(value => {
  console.log(value);
  return value % 2 === 0;
});
==> 1
==> 2
console.log(returnValue3);
==> true





// 空配列の場合
const returnValue4 = [].some(value => {
  return true;
});
console.log(returnValue4);
==> false





// 真偽値(boolean)以外をコールバック関数が返す場合.1
const array5 = [true, false];
const returnValue5 = array5.some(function () {
  return 'FooBarNinja';
});
console.log(returnValue5);
==> true





// 真偽値(boolean)以外をコールバック関数が返す場合.2
const array6 = [true, false];
const returnValue6 = array6.some(function () {
  return '';
});
console.log(returnValue6);
==> false

要素のインデックス取得

「コールバック関数の第2引数」を指定すると、処理している配列の「要素のインデックス」を取得することができます。

const array1 = ['js', 0, true];
const returnValue1 = array1.some((value, index) => {
  console.log(index);
  return value === index;
});
==> 0
==> 1
==> 2
console.log(returnValue1);
==> false






const array2 = [false, 'some', 0];
const returnValue2 = array2.some((value, index) => {
  return value && index > 0;
});
console.log(returnValue2);
==> true

呼び出し元配列の取得

「コールバック関数の第3引数」を指定すると、呼び出し元の配列を取得することができます。

const array1 = [100, 200];
const returnValue1 = array1.some((value, index, array) => {
  console.log(array, array[index]);
});
==> [ 100, 200 ] 100
==> [ 100, 200 ] 200
console.log(returnValue1);
==> false

コールバック関数内でのthis指定

「someメソッドの第2引数」を指定することで、コールバック関数内で使用するthisを指定することができます。

コールバック関数をアロー関数で指定した場合はthisはコンテキストによって決定されることに注意してください。

function Person(name) {
  this.name = name;
}

Person.prototype.hasSameNamePerson = function (persons) {
  return persons.some(person => {
    return person.name === this.name;
  }, this);
};

const person1 = new Person('A');
const persons = [new Person('B'), new Person('A')];
const returnValue1 = person1.hasSameNamePerson(persons);
console.log(returnValue1);
==> true

1次情報

Array.prototype.some() - MDN web docs

this - MDN web docs