[英]How can I group objects with the same name using Javascript?
Using Node, I'm implementing a ID3 tag parser to get the title, album, and artist from MP3's. 使用Node,我正在实现一个ID3标签解析器来获取MP3的标题,专辑和艺术家。
Now, I need to take the information I've gotten back, and group them by album name. 现在,我需要获取我收到的信息,并按照专辑名称对它们进行分组。
In my specific usage, I'm trying to go from 在我的具体用法中,我试图从中走出来
[
{
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'
}
},
...
]
What would be the best way to do this? 最好的方法是什么? I'm relatively new to JS, so it may be simple. 我对JS比较新,所以可能很简单。
Just use Array.prototype.forEach()
只需使用Array.prototype.forEach()
The
forEach()
method executes a provided function once per array element.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>');
You can use Array.prototype.reduce 您可以使用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;
}, {});
OBS: 'Ashes Of The Wake' and 'Traitor' should be arrays, not objects, because they have multiple children. OBS:'Wake'和'Traitor'应该是数组,而不是对象,因为它们有多个孩子。
You don't have to, but in case you want to use underscore
, as @Daniel said, you can use the groupBy
function. 你没必要,但是如果你想使用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);
Will output: 将输出:
[
'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'
}
]
]
As Kieran suggested, using underscore.js provides you an easy elegant solution. 正如Kieran建议的那样,使用underscore.js为您提供了一个简单优雅的解决方案。
var groupedData = _.groupBy(data, 'album');
Here's the plunker: http://plnkr.co/edit/a7DhxpGx0C4FVMgIjzPY?p=preview 以下是plunker: http ://plnkr.co/edit/a7DhxpGx0C4FVMgIjzPY?p = preview
I would like to add that you would want to use lodash instead of underscore if you are working with Node.js 我想补充一点,如果你正在使用Node.js,你会想要使用lodash而不是下划线
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.