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?

reduce()の使い方 JavaScript Vue.js

0
Last updated at Posted at 2026-09-27

基本の形

配列.reduce((第一引数, 第二引数) => {
  return 次の値
}, 初期値)

第一引数は繰り返し計算した合計値を保持するためのもの
第二引数 現在処理している1件
return ... 次の繰り返しに渡す値

例えばこのようなデータがあった場合

expenses.value = [
  {
    id: 0,
    date: '2026-09-27',
    title: 'スーパー',
    amount: 3000,
    category: 'food'
  },
  {
    id: 1,
    date: '2026-09-27',
    title: '美容院',
    amount: 5000,
    category: 'beauty'
  },
  {
    id: 2,
    date: '2026-09-27',
    title: '子どもの文房具',
    amount: 2000,
    category: 'child'
  }
]

このように使う。

expenses.value.reduce((total, expense) => {
  return thisMonth === expense.date.slice(0, 7)
    ? total + Number(expense.amount)
    : total
}, 0)

reduce()は配列の中にオブジェクトが入っている形だけでなく、配列のみでも使える。

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?