Last active
March 19, 2020 01:03
-
-
Save masatomix/9f55aa6f535833ae7352ac5a15a64a79 to your computer and use it in GitHub Desktop.
同じ key値のデータを、配列としてまとめる。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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