0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

複数言語のリスト全部要素一致と部分要素一致の書き方

Posted at

概要

リスト要素の全てがある条件に一致、またはどれか一つでも一致する場合を計算する事が多いが、都度書き方を調べているのでまとめた

JavaScript

// 全要素一致
const array = [1, 2, 3, 4, 5]
array.every(value => value < 10) // true

// 部分一致
array.some(value => value === 3)

Swift

// 全要素一致
let array = [1, 2, 3, 4, 5]
array.allSatisfy{ $0 < 10 }

// 部分一致
array.filter { $0 == 3 }.count > 0

Kotlin

// 全要素一致
val array = listOf(1, 2, 3, 4, 5)
array.all { it < 10 } // true

// 部分一致
array.any { it == 3 }

PHP

// 全要素一致
$array = [1, 2, 3, 4, 5]
$result = true
foreach($array as $value) {  // array_map でも可能
  if(!($value < 10)) {
    $result = false
  }
}

// 部分一致
array_filter($array, function($value) {
  return $value === 3
})
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?