var list = [
    {color:"red",    size:"x", count:4},
    {color:"red",    size:"l", count:3},
    {color:"yellow", size:"l", count:2},
    {color:"red",    size:"x", count:1},
    {color:"blue",   size:"x", count:3},
    {color:"yellow", size:"l", count:2}
]//希望返回如下数组var return = [
    {color:"red",    size:"x", count:5},
    {color:"red",    size:"l", count:3},
    {color:"yellow", size:"l", count:4},
    {color:"blue",   size:"x", count:3},
]//即 color和size相同的.合并为同一个对象,更改count,无需排序,谢谢.

解决方案 »

  1.   

    写了一个,你看看:var list = [
        {color:"red",    size:"x", count:4},
        {color:"red",    size:"l", count:3},
        {color:"yellow", size:"l", count:2},
        {color:"red",    size:"x", count:1},
        {color:"blue",   size:"x", count:3},
        {color:"yellow", size:"l", count:2}
    ];
    var res=[];
    for(var i=0,temp={};i<list.length;i++) {
    var cur=list[i];
    var t=temp[cur.color+"|"+cur.size];
    if(typeof(t)!="undefined" && res[t[0]].color==cur.color && res[t[0]].size==cur.size) {
    res[t[1]].count+=cur.count;
    }else {
    res.push(cur);
    temp[cur.color+"|"+cur.size]=[i,res.length-1];
    }
    }//输出测试
    var arr=[];
    for(var i=0;i<res.length;i++) {
    arr.push(res[i].color+"|"+res[i].size+"|"+res[i].count);
    }
    document.write(arr.join("<br/>"));
      

  2.   


    <script type="text/javascript">
    <!--
    var list = [
        {color:"red",    size:"x", count:4},
        {color:"red",    size:"l", count:3},
        {color:"yellow", size:"l", count:2},
        {color:"red",    size:"x", count:1},
        {color:"blue",   size:"x", count:3},
        {color:"yellow", size:"l", count:2}
    ];var count = 0, arr = [];
    for(var j = 0; j < list.length; j++){
    list[j].index = j;
    }
    function By(color, size){
    return function(o, p){
    var a, b, c, d;
    if(typeof o === 'object' && typeof p === 'object' && o && p){
     a =  o[color]; b = p[color]; c = o[size]; d = p[size];
     if((a == b) && (c == d)){
     o['count'] += p['count'];
     arr.push(p.index);
     }
    }
    return 0;
    }
    }list.sort(By('color', 'size'))
    for(var i = 0; i < arr.length; i++){
    list.splice(arr[i], 1);
    }
    for(var k = 0; k < list.length; k++){
    document.write(list[k].color + "--->" + list[k].size + "--->" + list[k].count + "<br/>");
    }//希望返回如下数组/*var return = [
        {color:"red",    size:"x", count:5},
        {color:"red",    size:"l", count:3},
        {color:"yellow", size:"l", count:4},
        {color:"blue",   size:"x", count:3},
    ]*///-->
    </script>
      

  3.   

    <script>var list = [
        {color:"red",    size:"x", count:4},
        {color:"red",    size:"l", count:3},
        {color:"yellow", size:"l", count:2},
        {color:"red",    size:"x", count:1},
        {color:"blue",   size:"x", count:3},
        {color:"yellow", size:"l", count:2}
    ]var hash = {},result=[];
    for(var i=0,n=list.length;i<n;i++){
    var l=list[i],name=l.color+"_"+l.size;
    if(hash[name]){
    hash[name]+=l.count;
    }else{
    hash[name]=l.count;
    }
    }
    for(var name in hash){
    var n=name.split("_")
    result.push({color:n[0], size:n[1], count:hash[name]});
    }
    for(var i=0,n=result.length;i<n;i++){
    var r=result[i];
    document.write("{color:"+r.color+", size:"+r.size+", count:"+r.count+"}<br>")
    }</script>
      

  4.   

    反向循环嵌套合并,算不算优雅?!L@_@K
    var list = [
        {color:"red",    size:"x", count:4},
        {color:"red",    size:"l", count:3},
        {color:"yellow", size:"l", count:2},
        {color:"red",    size:"x", count:1},
        {color:"blue",   size:"x", count:3},
        {color:"yellow", size:"l", count:2}
    ];var cur, sibling;for (var i=list.length-1; i >= 0; i--)
    {
        cur = list[i];    for (var j=i-1; j >= 0; j--)
        {
            sibling = list[j];        if (cur.color == sibling.color 
                && cur.size == sibling.size)
            {
                sibling.count += cur.count;
                list.splice(i, 1);
                break;
            }
        }
    }alert(list.length);
    for (var i=0; i<list.length; i++)
    {
        document.write("color: ", list[i].color
                      ,", size: ", list[i].size
                      ,", count:", list[i].count
                      ,"<br />");
    }
      

  5.   

    晕,俺修改了原数组的元素,lz 希望这样么?!不过这引出一个新问题,JS 数组如何实现深度复制?!
      

  6.   


    递归一下就行啦
    参考那个deepextend
      

  7.   

    哈,多谢顺便问下,cloudgamer 怎么翻译?云游者?!
      

  8.   

    to #4 sohighthesky 谢谢你的回复,你的代码我看懂了. 不过一个判断里面好像有点错误,  if(typeof(t)!="undefined" && res[t[0]].color==cur.color && res[t[0]].size==cur.size)这里可能得用 res[t[1]].color  res[t[1]].size 来对比,不知道是不是我搞错了.
      

  9.   

    是的,疏忽了,我原本是想这么写的:list[t[0]].color==cur.color && list[t[1]].size==cur.size
      

  10.   

    既然这样后面的值就不用数组了:var res=[];
    for(var i=0,temp={};i<list.length;i++) {
    var cur=list[i];
    var t=temp[cur.color+"|"+cur.size];
    if(typeof(t)!="undefined" && res[t].color==cur.color && res[t].size==cur.size) {
    res[t].count+=cur.count;
    }else {
    res.push(cur);
    temp[cur.color+"|"+cur.size]=res.length-1;
    }
    }