简体   繁体   English

在Coffeescript中,如何将一个对象的数组按一个属性分组?

[英]In Coffeescript, how would I group an array of objects by one property?

Let's say I have an array of objects like this: 假设我有一个这样的对象数组:

var array = [
    {name:"aaa", height:"20"},
    {name:"bbb", height:"100"},
    {name:"ccc", height:"20"},
    {name:"ddd", height:"20"},
    {name:"eee", height:"100"},
]

I want to group it by height in this case, so I end up with a different array that has the group name and then each item in that group like so: 在这种情况下,我想按高度对它进行分组,所以最终得到一个具有组名的数组,然后是该组中的每个项目,如下所示:

var grouped_by_height = [
    [{name:"aaa", height:"20"}, {name:"ccc", height:"20"}, {name:"ddd", height:"20"}],
    [{name:"bbb", height:"100"}, {name:"eee", height:"100"}]
]

I've written a long solution in JS/jQuery but I was wondering if there was a quick and easy way to do this is Coffeescript. 我已经用JS / jQuery写了一个很长的解决方案,但我想知道是否有一种快速简便的方法是Coffeescript。

I don't think there's anything special in CoffeeScript that would help you (or at least nothing that takes care of the hard parts). 我认为CoffeeScript中没有什么特别的东西可以帮助您(或者至少没有什么可以解决困难的部分)。 I'd probably use Array.prototype.reduce to do the heavy lifting with something like this: 我可能会使用Array.prototype.reduce来完成以下工作:

group_by_height = (groups, obj) ->
    groups[obj.height] ?= [ ]
    groups[obj.height].push(obj)
    groups
grouped_obj = array.reduce(group_by_height, { })
grouped_by_height = (v for k, v of grouped_obj)

That won't guarantee any particular order in grouped_by_height though but that can be remedied without much effort by adding a sorting step: 但是,这不能保证grouped_by_height任何特定顺序,但是可以通过添加排序步骤来进行纠正,而无需付出很多努力:

by_height = (a, b) -> +a[0].height - +b[0].height
group_by_height = (v for k, v of grouped_obj).sort(by_height)

You'd be sorting an array-of-arrays so a and b would be arrays, hence the [0] to look at the first elements. 您将要对数组进行排序,因此ab将是数组,因此[0]用于查看第一个元素。 The unary + operators are there to convert your string heights to numeric heights so that they'll compare the way you probably expect them to. 一元+运算符可将您的字符串高度转换为数字高度,以便它们可以比较您可能期望的方式。

This is what I was able to come up with. 这就是我能想到的。 As mu is too short stated, there is no magic in coffeescript for it but there are some built in things that help it out. 由于亩太短了,所以在咖啡文字中没有魔术,但有一些内置的东西可以帮助它。 Only issue is that the compiled js version ends up being longer than if you just wrote it in plain js. 唯一的问题是,与仅用普通js编写相比,编译后的js版本最终会更长。

groupByKey = (array, key) ->
  grouped = {}
  for obj in array
    grouped[obj[key]] ?= []
    grouped[obj[key]].push obj
  Object.keys(grouped).map (group) ->
    grouped[group]

Try it out for yourself: 自己试试吧:

array = [
  {
    name: 'aaa'
    height: '20'
  }
  {
    name: 'bbb'
    height: '100'
  }
  {
    name: 'ccc'
    height: '20'
  }
  {
    name: 'ddd'
    height: '20'
  }
  {
    name: 'eee'
    height: '100'
  }
]

groupByKey = (array, key) ->
  grouped = {}
  for obj in array
    grouped[obj[key]] ?= []
    grouped[obj[key]].push obj
  Object.keys(grouped).map (group) ->
    grouped[group]

console.log groupByKey(array, 'height')

The output is: 输出为:

[
  [
    { 
      name: 'aaa',
      height: '20' 
    },
    { 
      name: 'ccc',
      height: '20' 
    },
    { 
      name: 'ddd',
      height: '20' 
    } 
  ],
  [ 
    { 
      name: 'bbb',
      height: '100'
    },
    { 
      name: 'eee',
      height: '100' 
    }
  ]
]
array = [
    {name:"aaa", height:"20"},
    {name:"bbb", height:"100"},
    {name:"ccc", height:"20"},
    {name:"ddd", height:"20"},
    {name:"eee", height:"100"},
]

# unique heights
uniq = array.reduce (memo, el) ->
  memo.push(el.height) if memo.indexOf(el.height) is -1
  memo
, []

# output grouped by height
out = [[obj for obj in array when obj.height is height][0] for height in uniq][0]

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

相关问题 如何按属性将对象数组分组为 ARRAYS 数组? - How do I group an array of objects into an array of ARRAYS by a property? 如何使用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 aggregate/group an Array of Objects by property value? 如何通过嵌入在该数组的属性中的单个数组属性对对象数组进行分组 - How do I group an array of objects by a singular array property embedded within a property of that array CoffeeScript-如何在类中检索静态数组属性 - CoffeeScript - How do I retrieve a static array property in class 按属性将数组中的对象分组 - Group objects in array by property 如何按某些属性值对对象数组进行分组 - How to group array of objects by certain property values 如何根据对象的属性对对象数组进行分组 - How to group an array of objects according to a property in that object 我如何在coffeescript中编写这个jQuery? - How would I write this jQuery in coffeescript? 我将如何防止对象数组中的重复项? 如果有一个具有相同属性的对象,它应该求和它的值 - How would I prevent duplicates in array of objects? And if there is a object with same property, it should sume its value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM