我有一些带有一些数据的未排序数组,我想创建新的数组宽度排序数据:
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"
};
}
未排序的数据看起来像对象数组
我希望将其归类为
//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]
依旧...... 我已经尝试过array.map和reduce,但它们似乎不适用于IE 我也试过了:
$.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);
其中aggregation_content是
{cell:"ProjectOwner",value:""},{cell:"ProjectName",value:""},{cell:"ProjectLeader",value:""},{cell:"ProjectType",value:""}
并且新数据从一开始就是gridData。 这适用于第一个聚合。但问题是当我聚合数组newData时我需要使用
if(! (n in newData[o]))
newData[o][n] = [];
newData[o][n].push(objA);
并且[o]应该是[n] -node的父级。 ok-第二组也可以使用该代码,但是当我想制作5个内部组时,我需要像
那样做newData["firstGroup"]["secondGroup"]["thirdGroup"]...[n].push("Some content").
如何以编程方式进行此操作?如果我做
newData = newData[o] - no good
or
temp = newData
do something to temp
newData[o] = temp not good eather :'(
我希望我写出可理解的文字:D
____ Edited_2012_12_13 ______________________________________
所以输入数据是
[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
....
输出数据应该类似于
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
...
...
树视图应该是两个或多个嵌套节点。
答案 0 :(得分:1)
我之前遇到过类似的情况。我写了一个库来做到这一点:
https://github.com/raghavv/array-mod
请检查一下。我不是在推广我的图书馆,但我认为它符合您的要求。
此外,对于较旧的浏览器支持IE&lt; 9.您还需要包括另一个优秀的图书馆:
https://github.com/kriskowal/es5-shim/
以下是如何获得第一组数组:
$(gridData).findAll(“Corporation1”,“ProjectOwner”);
$(gridData).findAll(“Corporation2”,“ProjectOwner”);
$(gridData).findAll(“A1”,“ProjectName”);
$(gridData).findAll(“B2”,“ProjectName”);
答案 1 :(得分:0)
我想出了这个结果:
this.groupBy = function(originalData, aggregation_content)
{
var parentNode,node,i,j,g;
var newData = {},temp;
this.array.sortedData = originalData;
$.each(aggregation_content,function(i,objI){
$.each(originalData.Items,function(j,objJ){
node = originalData.Items[j].GetValue(aggregation_content[i].column).replace(/\W+/gi,"");
if(i > 0)
{
temp = newData;
$.each(aggregation_content,function(g,objG){
parentNode = originalData.Items[j].GetValue(aggregation_content[g].column).replace(/\W+/gi,"");
if(!temp[parentNode])
{
temp[parentNode] = [];
}
temp = temp[parentNode];
});
temp.push(objJ);
}
else
{
if(!newData[node])
newData[node] = [];
}
});
});
console.log(newData);
return newData;
}
我所做的是使用指针填充新的treeview(multidimencional)数组。 所以输出按aggregation_content分组。
答案 2 :(得分:0)
使用循环计数器根据长度分组:
var foo = [57657,57751,58401,58420,58588,58655,59238,59443,59488,59492,59570,59706,59924,59925, 57674, 57687, 57688, 57689, 57693, 57770, 57785, 57786, 57796, 57798, 57810, 57827, 57829, 57835, 57850, 57851, 57852, 57903, 57909, 57910,57957, 57972, 57998, 58022, 58046, 58059, 58064, 58077, 58085, 58097, 58103, 58105, 58127, 58138, 58139, 58220, 58320, 58353, 58356, 58357,58358, 58359, 58360, 58402, 58403, 58404, 58467, 58472, 58473, 58493, 58605]
/* Group into sets of twenty */
var i = 0; while (i <= foo.length) { Math[i] = foo.slice(i, i+20).toString(); i = i + 20; }