Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript array: how to group

I have unsorted array with some data and I want to create new array width sorted data:

   var gridData = [], i = -1;
        while (i++ < 5) {
                gridData[i] = {
                    "ProjectOwner": ["Corporation1","Corporation2","Corporation3"][i % 2],
                    "ProjectId": i,
                    "ProjectName": String.fromCharCode("A".charCodeAt(0) + i % 3) + (1 + i % 3),
                    "ProjectType": ["Conference","Programming","Research","Conference"][i % 4],
                    "ProjectLeader": "Name1Name2Name3".substr(i % 3 * 5,5) + " " + "Surname1Surname2Surname3".substr(i % 3 * 8,8),
                    "StartDate": new Date(2009 + i % 4 % 2, (i + 3) % 12, 5 + i * 4 % 24).toLocaleDateString(),
                    "EndDate": new Date(2010 + i % 4 % 2, (i + 3) % 12, 5 + i * 4 % 24).toLocaleDateString(),
                    "Summary": "bla bla"
                };
            }

unsorted data looks like object array

and I would like to group it like

//First group by ProjectOwner
Object {Corporation1: Array[3], Corporation2: Array[3]}
   //Second group by ProjectName
   Corporation1: Array[x]
            A1 : Array [x]
                 Object1
                 Object2
                 ...
   Corporation2: Array[x]

and so on... I have tried array.map and reduce but they don't seem to work in IE also I have tried:

$.each(aggregation_content, function(i,aggr) {

    $.each(_newData, function(a, objA) {
        if(i > 0)
            o = _newData[a][aggregation_content[i - 1].cell];
        n = _newData[a][aggregation_content[i].cell];

        if(typeof o == "undefined") //For first row when new Data is empty array
        {
            if(! (n in newData))
                newData[n] = [];
            newData[n].push(objA);
        }
    })})
console.log(newData);

where aggregation_content is

{cell:"ProjectOwner",value:""},{cell:"ProjectName",value:""},{cell:"ProjectLeader",value:""},{cell:"ProjectType",value:""}

and new data is a gridData from the beginning. this works perfectly for a first aggregation. but the problem is that when I aggregate the array newData I need to use

if(! (n in newData[o]))
    newData[o][n] = [];
 newData[o][n].push(objA);

and that [o] should be a parent of [n] -node. ok- second group is ok too with that code, but when I want to make 5 inner groups I need to do it like

newData["firstGroup"]["secondGroup"]["thirdGroup"]...[n].push("Some content").

How can this be made programmatically? If I do

newData = newData[o] - no good
or
temp = newData
do something to temp
newData[o] = temp not good eather :'(

I hope I wrote understandable text :D

____Edited_2012_12_13______________________________________

So the input data is

[Object, Object, Object, Object, Object, Object]
  0: Object
   EndDate: "Monday, April 05, 2010"
   ProjectId: 0
   ProjectLeader: "Name1 Surname1"
   ProjectName: "A1"
   ProjectOwner: "Corporation1"
   ProjectType: "Conference"
   StartDate: "Sunday, April 05, 2009"
   Summary: "bla bla"
   __proto__: Object
 1: Object
   EndDate: "Monday, May 09, 2011"
   ProjectId: 1
   ProjectLeader: "Name2 Surname2"
   ProjectName: "B2"
   ProjectOwner: "Corporation2"
   ProjectType: "Programming"
   StartDate: "Sunday, May 09, 2010"
   Summary: "bla bla"
   __proto__: Object
 2: Object
   ...
 3: Object
    ...
 4: Object
    ...
 5: Object
    ....

And the output data should be something like

Object {Corporation1: Array[3], Corporation2: Array[3]}
 Corporation1: Array[2]
   0: A1:  Array[X]
         0: Object
            EndDate: "Monday, May 09, 2011"
            ProjectId: 1
            ProjectLeader: "Name2 Surname2"
            ProjectName: "A1"
            ProjectOwner: "Corporation2"
            ProjectType: "Programming"
            StartDate: "Sunday, May 09, 2010"
            Summary: "bla bla"
         1: Object
            ...
            ...
   1: B2:  Array[X]
         0: Object
            EndDate: "Monday, May 09, 2011"
            ProjectId: 1
            ProjectLeader: "Name2 Surname2"
            ProjectName: "B2"
            ProjectOwner: "Corporation2"
            ProjectType: "Programming"
            StartDate: "Sunday, May 09, 2010"
            Summary: "bla bla"
         1: Object
            ...
            ...
 Corporation2: Array[2]
   0: A1:  Array[X]
         0: Object
            EndDate: "Monday, May 09, 2011"
            ProjectId: 1
            ProjectLeader: "Name2 Surname2"
            ProjectName: "A1"
            ProjectOwner: "Corporation2"
            ProjectType: "Programming"
            StartDate: "Sunday, May 09, 2010"
            Summary: "bla bla"
         1: Object
            ...
            ...
   1: B2:  Array[X]
         0: Object
            EndDate: "Monday, May 09, 2011"
            ProjectId: 1
            ProjectLeader: "Name2 Surname2"
            ProjectName: "B2"
            ProjectOwner: "Corporation2"
            ProjectType: "Programming"
            StartDate: "Sunday, May 09, 2010"
            Summary: "bla bla"
         1: Object
            ...
            ...

The tree view should be two or more nested nodes.

like image 251
Jan Avatar asked Mar 24 '26 16:03

Jan


1 Answers

I have suffered similar situation earlier. I wrote a library to do this:

https://github.com/raghavv/array-mod

Please check it. I am not promoting my library but I think it fits your requirement.

Also, for older browser support IE < 9. You will also need to include another excellent library:

https://github.com/kriskowal/es5-shim/

Here is how you get your first set of arrays:

a$(gridData).findAll("Corporation1", "ProjectOwner");
a$(gridData).findAll("Corporation2", "ProjectOwner");
a$(gridData).findAll("A1", "ProjectName");
a$(gridData).findAll("B2", "ProjectName");

like image 139
closure Avatar answered Mar 27 '26 06:03

closure



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!