简体   繁体   English

如何使用javascript通过对象属性将数组拆分为数组?

[英]How to split an array into arrays by an objects property using javascript?

I currently have an array that resembles the array below: 我目前有一个类似于下面数组的数组:

[
      {id:1,color:'red'},
      {id:2,color:'blue'},
      {id:3,color:'red'},
      {id:4,color:'green'},
      {id:5,color:'blue'},
]

I am looking for the fastest way to get something like below where I could split/sort the array by a property in the object, in this example it would be 'color' : 我正在寻找最快的方法来获得类似下面的东西,我可以通过对象中的属性对数组进行拆分/排序,在这个例子中它将是'color':

[
      [
        {id:1,color:'red'},
        {id:3,color:'red'},
      ],[
        {id:2,color:'blue'},
        {id:5,color:'blue'},
      ],[
        {id:4,color:'green'},
    ]
]

I could write a function for this but I was thinking there may be something to do this already in underscore.js, but had no luck finding it. 我可以为此编写一个函数,但我认为在underscore.js中可能有一些事情要做,但没有找到它的运气。

You need to use the groupBy function of underscore you can find it in the docs right here . 您需要使用下划线的groupBy函数,您可以在此处的文档中找到它。

groupBy_.groupBy(list, iterator, [context]) groupBy_.groupBy(list,iterator,[context])
Splits a collection into sets, grouped by the result of running each value through iterator. 将集合拆分为集合,按照通过迭代器运行每个值的结果进行分组。 If iterator is a string instead of a function, groups by the property named by iterator on each of the values. 如果迭代器是字符串而不是函数,则按每个值上的迭代器命名的属性进行分组。

[
      {id:1,color:'red'},
      {id:2,color:'blue'},
      {id:3,color:'red'},
      {id:4,color:'green'},
      {id:5,color:'blue'},
]

_.groupBy(a, function(x){ return x.color; });

You can combine the _.groupBy with the _.toArray function: 您可以将_.groupBy_.toArray函数结合使用:

var dict = [
      {id:1,color:'red'},
      {id:2,color:'blue'},
      {id:3,color:'red'},
      {id:4,color:'green'},
      {id:5,color:'blue'},
];

var grouped = _.groupBy(dict, function(x){ return x.color; });
var result = _.toArray(grouped);

You can get the required results using ES5 features without need of third party libraries: 您可以使用ES5功能获得所需的结果,而无需第三方库:

var data = [
      {id:1,color:'red'},
      {id:2,color:'blue'},
      {id:3,color:'red'},
      {id:4,color:'green'},
      {id:5,color:'blue'},
];

var temp = {};
data.forEach(function(item){
    if (temp[item.color]) { 
         temp[item.color].push(item);
     } else {
       temp[item.color] = [ item ]; }   
});

var result = Object.keys(temp).map(function(key){ return temp[key]; });

In order to support older browsers use ES5 shims 为了支持旧浏览器,请使用ES5填充程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 arrays Javascript 拆分对象数组 - How to split an array of objects by arrays Javascript 根据属性将 javascript 中的对象数组拆分为单独的 arrays - split array of objects in javascript into separate arrays based on a property 如何通过对象数组 javascript 中的特定公共属性合并对象的 arrays - how merge arrays of objects by specific common property in on array of objects javascript Javascript:根据属性值将对象数组拆分为具有动态名称的单独数组 - Javascript: Split array of objects into seperate arrays with dynamic names depending on property value 使用 javascript 将关联对象分离或拆分为 2 个 arrays - Separate or split asociative objects into 2 arrays using javascript 如何将一个对象数组拆分成多个普通数组,其中每个数组都由一个属性的值填充? - How to split an array of objects into multiple normal arrays where each array is populated by one property's values? 如何通过使用 javascript 比较对象数组和 arrays 来获得不匹配的值 - how to get not matching value by comparing array of objects and arrays using javascript javascript 将 arrays 的 object 拆分为给定大小的对象数组 - javascript split an object of arrays into an array of objects given a size 如何使用 JavaScript 将长数组拆分为更小的 arrays - How to split a long array into smaller arrays, with JavaScript 如何在javascript中将数组拆分为两个数组? - how to split an array into two arrays in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM