简体   繁体   English

如何使用Javascript将具有相同名称的对象分组?

[英]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 在我的具体用法中,我试图从中走出来

This: 这个:

[
  { 
    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: (pseudo code) 对此:(伪代码)

[
  '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'
    }
  ]
]

JSFiddle 的jsfiddle

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

Edit 编辑

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.

相关问题 javascript中可以有多个具有相同名称的对象吗? - Can there be multiple objects with the same name in javascript? 如何计算数组中同名对象的数量? - How can I count the amount of objects with the same name in array? 如何查看两个File对象是否使用JavaScript引用相同的物理文件? - How can I see if two File objects refer to the same physical file using JavaScript? 如何验证 2 个 JavaScript 对象是否具有相同的键 - How can I verify if 2 JavaScript Objects have the same keys or not 如何检查两个javascript对象是否具有相同的字段? - How can I check if two javascript objects have the same fields? 如何在 JavaScript 的对象数组中将子对象递归分组? - How can i recursively group children objects under parent in an array of objects in JavaScript? 将具有相同属性的对象分组 - javascript - Group objects with same property - javascript 如何使用JQuery / JavaScript将对象数组中的值分组并从这些组中创建新的对象数组 - Using JQuery/JavaScript how would I group values in an array of objects and create a new array of objects from the groups 如何使用对象名称值访问数组? - How can I access an array using an objects name value? 在Javascript对象数组中,如果该元素本身是一个对象数组,如何通过名称/ ID来定位特定的数组元素? - In a Javascript array of objects, how can I target a specific array element by name/ID if that element is an array of objects itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM