LoginSignup
1
0

More than 5 years have passed since last update.

Javascriptで特定の要素を取り除く

Posted at

indexOfとspliceを使って行う
indexOfで位置を見つける
spliceで要素を削除する

Code


const arr = [10,20,30,4,5,6,112,333,4,2,5,66];

function deleteElement(arr, targetNum) {
  let position = arr.indexOf(tagetNum);
  if (position === -1) {
    return null
  }
  arr.splice(position, 1);
}

1
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
1
0