【问题标题】:How can I group objects with the same name using Javascript?如何使用 Javascript 对具有相同名称的对象进行分组?
【发布时间】:2015-12-11 03:43:29
【问题描述】:

使用 Node,我正在实现一个 ID3 标签解析器,以从 MP3 中获取标题、专辑和艺术家。

现在,我需要取回我得到的信息,并按专辑名称对它们进行分组。

在我的具体用法中,我试图从

这个:

[
  { 
    title: 'Break You',
    artist: 'Lamb Of God',
    album: 'Ashes Of The Wake' 
  },
  {
    title: 'An Extra Nail For Your Coffin',
    artist: 'Lamb Of God',
    album: 'Ashes Of The Wake' 
  },
  {
    title: 'Envy And Doubt',
    artist: 'Sever The King',
    album: 'Traitor' 
  },
  {
    title: 'Self Destruct',
    artist: 'Sever The King',
    album: 'Traitor' 
  },
  ...
]

至此:(伪代码)

[
  'Ashes Of The Wake'{
    {
      title: 'Break You',
      artist: 'Lamb Of God'
    },
    {
      title: 'An Extra Nail For Your Coffin',
      artist: 'Lamb Of God'
    }  
  }
  'Traitor'{
    {
      title: 'Envy and Doubt',
      artist: 'Sever The King'
    },
    {
      title: 'Self Destruct',
      artist: 'Sever The King'
    }
  },
  ...
]

最好的方法是什么?我对JS比较陌生,所以可能很简单。

【问题讨论】:

  • underscorejs 有 groupBy 如果你想使用它。您想要的输出是无效的 javascript 仅供参考。
  • 这是实际数据吗?我不相信'An Extra Nail For Your Coffin'来自那张专辑。 :)
  • @JesseKernaghan 你让我担心我的 ID3 解析器坏了! :) 但是查了一下,这首歌在《Ashes of the Wake》中

标签: javascript json node.js electron


【解决方案1】:

只需使用Array.prototype.forEach()

forEach() 方法对每个数组元素执行一次提供的函数。

var data = [{ title: 'Break You', artist: 'Lamb Of God', album: 'Ashes Of The Wake' }, { title: 'An Extra Nail For Your Coffin', artist: 'Lamb Of God', album: 'Ashes Of The Wake' }, { title: 'Envy And Doubt', artist: 'Sever The King', album: 'Traitor' }, { title: 'Self Destruct', artist: 'Sever The King', album: 'Traitor' }],
    grouped = {};

data.forEach(function (a) {
    grouped[a.album] = grouped[a.album] || [];
    grouped[a.album].push({ title: a.title, artist: a.artist });
});
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');

【讨论】:

    【解决方案2】:

    您可以使用Array.prototype.reduce

    var output = input.reduce(function(result, value) { 
      result[value.album] = result[value.album] || []; 
      result[value.album].push({ title: value.title, artist: value.artist });
      return result; 
    }, {});
    

    【讨论】:

      【解决方案3】:

      正如 Kieran 所建议的,使用 underscore.js 为您提供了一个简单而优雅的解决方案。

      var groupedData = _.groupBy(data, 'album');
      

      这里是 plunker:http://plnkr.co/edit/a7DhxpGx0C4FVMgIjzPY?p=preview

      编辑

      我想补充一点,如果您使用的是 Node.js,您可能希望使用 lodash 而不是下划线

      【讨论】:

      • 感谢这位朋友!它对我有用!
      【解决方案4】:

      OBS:“Ashes Of The Wake”和“Traitor”应该是数组,而不是对象,因为它们有多个孩子。

      您不必这样做,但如果您想使用underscore,正如@Daniel 所说,您可以使用groupBy 函数。

      var data = [
        { 
          title: 'Break You',
          artist: 'Lamb Of God',
          album: 'Ashes Of The Wake' 
        },
        {
          title: 'An Extra Nail For Your Coffin',
          artist: 'Lamb Of God',
          album: 'Ashes Of The Wake' 
        },
        {
          title: 'Envy And Doubt',
          artist: 'Sever The King',
          album: 'Traitor' 
        },
        {
          title: 'Self Destruct',
          artist: 'Sever The King',
          album: 'Traitor' 
        }
      ];
      var groupedData = _.groupBy(data, 'album');
      console.log(groupedData);
      

      将输出:

      [
        'Ashes Of The Wake': [
          {
            title: 'Break You',
            artist: 'Lamb Of God'
          },
          {
            title: 'An Extra Nail For Your Coffin',
            artist: 'Lamb Of God'
          }  
        ],
        'Traitor': [
          {
            title: 'Envy and Doubt,
            artist: 'Sever The King'
          },
          {
            title: 'Self Destruct',
            artist: 'Sever The King'
          }
        ]
      ]
      

      JSFiddle

      【讨论】:

      • 感谢您的解决方案!我对 JS 真的很陌生,所以我不知道对象不能有多个孩子。很高兴知道!
      猜你喜欢
      • 2020-09-06
      • 2018-10-24
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 2020-02-22
      相关资源
      最近更新 更多