LoginSignup
0
0

More than 3 years have passed since last update.

Javascript find()関数とfindIndex()関数の使い方 (走り書き)

Last updated at Posted at 2020-07-24

findとfindIndexの使い方

参考記事
https://ginpen.com/2018/12/04/array-find/

find(配列内の指定した数値や文字を取得)


const a = 'blue'
const ary = ['red', 'green','blues', 'blue', 'yellow', 'white','blue-bird']

// aと同じ要素があれば、抜き出します。
const result = ary.find(item => item === a)
console.log(result)
// result = 'blue'

findIndex(配列のインデックス(何番目か)を取得)


const ar = ['red', 'green','blues', 'blue', 'yellow', 'white','blue-bird']
const index = ar.findIndex(item =>item === 'red')//配列の0番から始まって何番目にあるかを抽出。見つからないと−1を返す
console.log(index)//結果:0(red以外を検索していたら、green:1, blues:2, blue: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