Skip to content

Instantly share code, notes, and snippets.

@masatomix
Last active March 19, 2020 01:03
Show Gist options
  • Save masatomix/9f55aa6f535833ae7352ac5a15a64a79 to your computer and use it in GitHub Desktop.
Save masatomix/9f55aa6f535833ae7352ac5a15a64a79 to your computer and use it in GitHub Desktop.
同じ key値のデータを、配列としてまとめる。
const re: { [key: string]: any[] } = {}
const datas = [
{ key: 'a', id: 'a_id1', value: '11' },
{ key: 'a', id: 'a_id2', value: '12' },
{ key: 'a', id: 'a_id3', value: '13' },
{ key: 'b', id: 'b_id1', value: '21' },
{ key: 'b', id: 'b_id2', value: '22' },
]
for (const row of datas) {
if (!re[row.key]) {
re[row.key] = []
}
const data = {
id: row.id,
value: row.value,
}
re[row.key].push(data)
}
console.log(re)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment