electron 如何使用JavaScript将同名对象分组?

0h4hbjxa  于 11个月前  发布在  Electron
关注(0)|答案(5)|浏览(144)

使用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' 
  },
  ...
]

字符串

To this:(伪代码)

[
  '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相对较新,所以它可能很简单。

hpxqektj

hpxqektj1#

使用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>');

字符串

vd2z7a6w

vd2z7a6w2#

可以使用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; 
}, {});

字符串

ndh0cuux

ndh0cuux3#

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

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

字符串
这是plunker:http://plnkr.co/edit/a7DhxpGx0C4FVMgIjzPY?p=preview

编辑

我想补充一点,如果你使用Node.js,你会希望使用lodash而不是下划线

odopli94

odopli944#

OBS:'Ashes Of The Wake'和'Traitor'应该是数组,而不是对象,因为它们有多个子级。
你不必这样做,但是如果你想使用underscore,就像@丹尼尔说的那样,你可以使用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

hfsqlsce

hfsqlsce5#

在普通的JavaScript中,可以将Object.groupBy与对象一起使用。

Object.groupBy(MUSIC_TRACKS, ({ album }) => album)

字符串
下面是一个示例:

const MUSIC_TRACKS = [{
    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'
  }
]

const groupedAlbum = Object.groupBy(MUSIC_TRACKS, ({ album }) => album)

// If you want to remove the "album" property from each object in the nested structure, 
// you need to take another iteration.
for (const key in groupedAlbum) {
  groupedAlbum[key].forEach(obj => {
    // Delete the "album" property from each object
    delete obj.album;
  });
}

console.log(groupedAlbum);

相关问题