4
2

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 1 year has passed since last update.

JavaScriptでグルーピング【Object.groupBy】

Posted at

reduce使わなくてもいいみたいです。

料理 = [
    { 名前: 'ぎょうざ',      種類: '中華料理' },
    { 名前: '担々麺',        種類: '中華料理' },
    { 名前: '肉じゃが',      種類: '和食' },
    { 名前: '寿司',          種類: '和食' },
    { 名前: 'スパゲッティー', 種類: 'イタリア料理' },
    { 名前: 'ピッツァ',      種類: 'イタリア料理' }
]

console.log(Object.groupBy(料理, ({ 種類 }) => 種類))
{
    "中華料理": [
        {
            "名前": "ぎょうざ",
            "種類": "中華料理"
        },
        {
            "名前": "担々麺",
            "種類": "中華料理"
        }
    ],
    "和食": [
        {
            "名前": "肉じゃが",
            "種類": "和食"
        },
        {
            "名前": "寿司",
            "種類": "和食"
        }
    ],
    "イタリア料理": [
        {
            "名前": "スパゲッティー",
            "種類": "イタリア料理"
        },
        {
            "名前": "ピッツァ",
            "種類": "イタリア料理"
        }
    ]
}

image.png

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?