日期:2014-05-16 浏览次数:20419 次
arr = [ {a:1 , b:1 , c:1}, {a:2 , b:1 , c:2}, {a:3 , b:3 , c:3},
{a:1 , b:1 , c:4}, {a:5 , b:5 , c:5}]
//变成
arr = [ {a:1,2 , b:1 , c:1,2,4}, {a:3 , b:3 , c:3}, {a:5 , b:5 , c:5}]
var unionObjectArray = function(arr, atr){
var r = [], t = {}, x, y, z;
for(var i = 0, il = arr.length; i < il; i++){
t[arr[i][atr]] = t[arr[i][atr]]
------解决方案--------------------
[];
t[arr[i][atr]].push(i)
}
for(var p in t)if(t.hasOwnProperty(p)){
x = {};
for(var i = 0, il = t[p].length; i < il; i++){
z = arr[t[p][i]];
for(var q in z)if(z.hasOwnProperty(q)){
x[q] = null;
}
}
for(var q in x)if(x.hasOwnProperty(q)){
y = {};
for(var i = 0, il = t[p].length; i < il; i++){
z = arr[t[p][i]];
if(z[q] != null) y[z[q]] = 1;
}
for(var s in y)if(y.hasOwnProperty(s)){
if(x[q] == null)x[q] = s;
else{
x[q] = [x[q]];
x[q].push(s)
}
&nb