On 9/21/06, Will <[EMAIL PROTECTED]> wrote:
>
> I am trying to make subgroups from an array of objects, based on a
> property value of each object.
>
> here is a sample data set:
> var Content = [
>                 {
>                   "id":12,
>                   "type":'foo'
>                   },
>                 {
>                   "id":9,
>                   "type":'bar'
>                   },
>                 {
>                   "id":4,
>                   "type":'foo'
>                   },
>                 {
>                   "id":10,
>                   "type":'foo'
>                   },
>                 {
>                   "id":2,
>                   "type":'other'
>                   },
>                 {
>                   "id":1,
>                   "type":'other'
>                   },
>                 {
>                   "id":3,
>                   "type":'bar'
>                   }
>                         ]
>
>
>
> The desired grouping i am trying to get is something like:
> var Grouped = [
>                 {
>                   "type": 'bar',
>                   "ids":[9,3]
>                   },
>                 {
>                   "type": 'foo',
>                   "ids":[12,4,10]
>                   },
>                 {
>                   "type": 'other',
>                   "ids":[1,2]
>                   }
>                         ]
>
> or something like:
>
> var Grouped = [
>                 {
>                   "type": 'bar',
>                   "objs":[{"id":9},{"id":3}]
>                   },
>                 {
>                   "type": 'foo',
>                   "objs":[{"id":12},{"id":4},{"id":10}]
>                   },
>                 {
>                   "type": 'other',
>                   "objs":[{"id":1},{"id":2}]
>                   }
>                         ]
>
>
> Looking at the Iter documentation, it seems like groupby() or
> groupby_as_array() would be a good tool to use to achieve this goal,
> but understanding how those work is definitely beyond my coding skill
> level. Are these the appropriate functions to use, and if so, could
> someone please explain for me how to set up this up. I was not able to
> follow how the example works.
>
> Thank you very much in advance!

It definitely doesn't have output that looks like what you want. All
of your examples create new objects that look very little like the
original objects.

Try groupby_as_array(Content, itemgetter("type")) -- that's all you're
going to get, anything else you have to write on your own.

-bob

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/mochikit
-~----------~----~----~----~------~----~------~--~---

Reply via email to