Skip to content

Instantly share code, notes, and snippets.

@momo-lab
Created July 31, 2024 01:29
Show Gist options
  • Save momo-lab/1d5cf09ee7c4feb5b377957c4c186551 to your computer and use it in GitHub Desktop.
Save momo-lab/1d5cf09ee7c4feb5b377957c4c186551 to your computer and use it in GitHub Desktop.
// 指定した条件の位置に値を追加する。合致しない場合は末尾に追加する。
Array.prototype.insertIf = function(value, condition) {
const index = this.findIndex(condition);
if (index < 0) {
this.push(value);
} else {
this.splice(index, 0, value);
}
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment