# 複数フィールドのchange系イベントハンドラを美しく書く

**URL:** <https://community.cybozu.dev/t/topic/3965>\
**Category:** kintone 開発相談\
**Created:** [2019 年 4 月 25 日午前 7:42 UTC](https://community.cybozu.dev/t/topic/3965 "2019-04-25T07:42:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account1665](https://avatars.discourse-cdn.com/v4/letter/l/f07891/32.png) [@Legacy\_Account1665](https://community.cybozu.dev/u/Legacy_Account1665)\
**Post date:** [2019 年 4 月 25 日午前 7:42 UTC](https://community.cybozu.dev/t/topic/3965/1 "2019-04-25T07:42:54Z")

</div>

[https://qiita.com/the\_red/items/ea2fa351c4d52d43fede](https://qiita.com/the_red/items/ea2fa351c4d52d43fede)

こんなQiita記事書いたので、リンク＆一部転載でーす。

&nbsp;

複数フィールドでchange系のイベントハンドラを使う際、こんな風にダラダラ長くなりがちですよね。

```
kintone.events.on(['app.record.create.show','app.record.edit.show','app.record.index.edit.show','app.record.create.change.first\_name','app.record.create.change.last\_name','app.record.create.change.full\_name','app.record.edit.change.first\_name','app.record.edit.change.last\_name','app.record.edit.change.full\_name','app.record.index.edit.change.first\_name','app.record.index.edit.change.last\_name','app.record.index.edit.change.full\_name',],event=\>{// ...})

```

&nbsp;

それが、reduce使うとこんなに美しくなります！

```
const FIELDS = ['first_name', 'last_name', 'full_name']
const EVENTS = FIELDS.reduce(
(events, field) => [
...events,
`app.record.create.change.${field}`,
`app.record.edit.change.${field}`,
`app.record.index.edit.change.${field}`,
],
['app.record.create.show', 'app.record.edit.show', 'app.record.index.edit.show']
)
kintone.events.on(EVENTS, event => {
// ...
})

```

&nbsp;

僕は美しいと思うのですが、どうでしょうか・・・？

中級者以上のみなさん、Array#reduce()をどんどん使っていきましょう！

[https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global\_Objects/Array/reduce](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)

&nbsp;

Qiita記事内ではさらに「krewSheet JavaScript APIとのロジック共通化」

も考慮したサンプルコード書いておりますので、よかったら読んでみてください〜。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1491](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Legacy\_Account1491](https://community.cybozu.dev/u/Legacy_Account1491)\
**Post date:** [2019 年 4 月 27 日午前 8:25 UTC](https://community.cybozu.dev/t/topic/3965/2 "2019-04-27T08:25:00Z")

</div>

美しい。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1665](https://avatars.discourse-cdn.com/v4/letter/l/f07891/32.png) [@Legacy\_Account1665](https://community.cybozu.dev/u/Legacy_Account1665)\
**Post date:** [2019 年 4 月 27 日午前 8:31 UTC](https://community.cybozu.dev/t/topic/3965/3 "2019-04-27T08:31:00Z")

</div>

ありがとうございます(//∇//)
