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?

More than 3 years have passed since last update.

javascript: filterとmap

Last updated at Posted at 2021-09-18

#はじめに
これはGASでスプレッドシートを処理する中で理解したことを忘れないための備忘録です

###データ
検証に利用する配列CurretGradeObject.createObjects()です。

CurretGradeObject.createObjects()
[
  { memberId: 100234,
    grade: '一般',
    award: '最優秀',
  },
  { memberId: 100567,
    grade: '一般',
    award: '優秀作',
  },
  { memberId: '',
    grade: '',
    award: '',
  }
]

#filter
### 書き方

var memberData = CurretGradeObject.createObjects().filter(function(x) {
    return x
})
返却値
[
  { memberId: 100234,
    grade: '一般',
    award: '最優秀',
  },
  { memberId: 100567,
    grade: '一般',
    award: '優秀作',
  },
  { memberId: '',
    grade: '',
    award: '',
  }
]

CurretGradeObject.createObjects()と同じ配列が返ってくるのでfilterの意味がない
xをループしてそのままxを返却した配列になる

###空は出力しない

var memberData = CurretGradeObject.createObjects().filter(function(x) {
    return x.memberId
})
返却値
[
  { memberId: 100234,
    grade: '一般',
    award: '最優秀',
  },
  { memberId: 100567,
    grade: '一般',
    award: '優秀作',
  }
]

###意味
memberIdに値のあるオブジェクトだけを絞り込んだ配列が返却される
スプレッドシート上の空行を除いてデータを取得したい場合に利用する

#map
###書き方

var memberIds = CurretGradeObject.createObjects().map(function(x) {
    return x.memberId
})
返却値
[
  100234,
    100567,
]

###意味
memberIdだけを抽出する

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?