如何使用 Javascript 将 objects 同名分组?
How can I group objects with the same name using Javascript?
我正在使用 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比较陌生,所以可能比较简单。
The forEach()
method executes a provided function once per array element.
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>');
OBS: 'Ashes Of The Wake' and 'Traitor' should be arrays, not objects, because they have multiple children.
你不必,但如果你想使用 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'
}
]
]
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;
}, {});
正如 Kieran 所建议的,使用 underscore.js 为您提供了一个简单而优雅的解决方案。
var groupedData = _.groupBy(data, 'album');
这里是 plunker:http://plnkr.co/edit/a7DhxpGx0C4FVMgIjzPY?p=preview
编辑
我想补充一点,如果您使用 Node.js
,您会希望使用 lodash 而不是下划线
我正在使用 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比较陌生,所以可能比较简单。
The
forEach()
method executes a provided function once per array element.
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>');
OBS: 'Ashes Of The Wake' and 'Traitor' should be arrays, not objects, because they have multiple children.
你不必,但如果你想使用 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'
}
]
]
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;
}, {});
正如 Kieran 所建议的,使用 underscore.js 为您提供了一个简单而优雅的解决方案。
var groupedData = _.groupBy(data, 'album');
这里是 plunker:http://plnkr.co/edit/a7DhxpGx0C4FVMgIjzPY?p=preview
编辑
我想补充一点,如果您使用 Node.js
,您会希望使用 lodash 而不是下划线