操作json进行分组再组

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>json 操作</title>
</head>

<body>
  <div id="oDiv"></div>
</body>
</html>
<script type="text/javascript" src="jquery.1.9.0.min.js"></script>
<script type="text/javascript">
var json={
    "data": {
        "secProducts": [
            {
                "name": "印度尼西亚Richeese纳宝帝奶酪威化饼干 58克",
                "type": 0
            },
            {
                "sellNum": 131,
                "type": 0
            },
            {
                "name": "越南Tipo奶油味面包干 300g",
                "type": 0
            },
            {
                "name": "泰国yumyum冬阴功杯面 70g",
                "type": 0
            },
            {
                "name": "丹麦Kjeldsens蓝罐曲奇 90g",
                "type": 0
            },
            {
                "name": "泰国进口Nanaco桔子味果冻240g",
                "type": 0
            },
            {
                "name": "奇多干杯脆粟米粒日式牛排味 25g",
                "type": 10
            },
            {
                "name": "德芙Dove丝滑牛奶颗粒巧克力 72g",
                "type": 10
            },
            {
                "name": "韩国进口海牌小力士鱼肠 ",
                "type": 10
            },
            {
                "name": "印尼丽芝士Richeese纳宝帝奶酪饼干 145克",
                "type": 10
            },
            {
                "name": "丹麦Kjeldsens蓝罐加仑子曲奇 90g ",
                "type": 10
            },
            {
                "name": "波兰费列罗椰蓉扁桃仁酥球 3粒装",
                "type": 10
            },
            {
                "name": "张君雅小妹妹休闲丸子 日式风味 80g",
                "type": 12
            },
            {
                "name": "新农哥熟纸皮核桃 200g/袋",
                "type": 12
            },
            {
                "name": "咪咪虾条 正宗马来西亚风味 20g*40包",
                "type": 12
            },
            {
                "name": "意大利Kinder费列罗牛奶榛果威化巧克力 43g ",
                "type": 12
            },
            {
                "name": "泰国Nanaco荔枝味果冻 240g",
                "type": 12
            },
            {
                "name": "德国Lorenz劳仑兹细薯条 100g",
                "type": 12
            },
            {
                "name": "阿尔卑斯精选混合口味棒棒糖 40g ",
                "type": 16
            },
            {
                "name": "德国Chio奇奥原味玉米片 125g",
                "type": 16
            },
            {
                "name": "美宝莲 好气色美肌霜 粉色30ml(CC霜 保湿 遮瑕 裸妆)",
                "type": 16
            },
            {
                "name": "德国Lorenz劳仑兹红椒细薯条 100g",
                "type": 16
            },
            {
                "name": "印尼丽芝士Richeese纳宝帝奶酪饼干 290g",
                "type": 16
            },
            {
                "name": "泰国 Nanaco芒果味果冻240g",
                "type": 16
            },
            {
              
                "name": "舞动舌尖 手撕面包起酥面包 原味40g 休闲零食品糕点",
              
                "type": 19
            },
            {
              
                "name": "乐天56%桶巧克力 86g",
          
                "type": 19
            },
            {
                "name": "韩国海地村鳕鱼肠 84g",
                "type": 19
            },
            {
                "name": "印度尼西亚Astick椰子味威化卷心酥 330g ",
                "type": 19
            },
            {
                "name": "德国Lorenz劳仑兹清新咸薯片 90g",
                "type": 19
            },
            {
                "name": "伊欧诗(EOS) 石榴覆盆子润唇球7g(润唇 滋润 不油腻 保湿 唇膏)",
                "type": 19
            },
            {
                "name": "旺旺仙贝原味105g",
                "type": 20
            },
            {
                "name": "好丽友好友趣三连包 111g/组",
             
                "type": 20
            },
            {
                "name": "ZEK蜂蜜黄油马铃薯片 60g",
                "type": 20
            },
            {
                "name": "泰国小老板调味海苔泰式咖喱蟹味 36g",
                "type": 20
            },
            {
                "name": "旺旺大米饼原味 400g",
                "type": 20
            },
            {
                "name": "谜尚(MISSHA)魅力润透妆前乳40g(保湿锁水 滋养肌肤 提亮肤色)",
                "type": 20
            },
            {
                "name": "乐事无限薯片忠于原味 104g",
                "type": 21
            },
            {
               
                "name": "台湾榙榙牛轧苏打夹心饼干蔓越莓味袋装156g",
               
                "type": 21
            },
            {
                "name": "韩国kkobangsi 高邦喜原味鳕鱼肠 135g(15g*9)",
                "type": 21
            },
            {
                "name": "雀巢Nestle脆脆鲨抹茶威化 320g",
                "type": 21
            },
            {
                "name": "明治Meiji巴旦木夹心黑巧克力 80g",
                "type": 21
            },
            {
                "name": "卡姿兰(Carslan)亮颜修容蜜粉02#淡粉色10g(散粉持久定妆控油遮瑕)",
                "type": 21
            },
            {
                "name": "新疆特产味正品新疆和田枣片 25g",
                "type": 22
            },
            {
                "name": "姚生记碧根果长寿果 128g/袋",
                "type": 22
            },
            {
                "name": "比利时德菲丝松露形代可可脂巧克力 100g",
               
                "type": 22
            },
            {
                "name": "韩国全南蜂蜜柚子茶 580g",
                "type": 22
            },
            {
                "name": "法国进口LU露怡牛奶巧克力饼干 150g",
                "type": 22
            },
             {
                "name": "百草味 牛肉干肉脯 五香牛肉粒 100g/袋",
                "product_promotion": {
                    "promotion_price": 18
                },
                "type": 10,
            },
             {
                "name": "百草味 牛肉干肉脯 五香牛肉粒 100g/袋",
                "product_promotion": {
                    "promotion_price": 18
                },
                "type": 12,
            },
             {
                "name": "百草味 牛肉干肉脯 五香牛肉粒 100g/袋",
                "product_promotion": {
                    "promotion_price": 18
                },
                "type":16 ,
            },
             {
                "name": "百草味 牛肉干肉脯 五香牛肉粒 100g/袋",
                "product_promotion": {
                    "promotion_price": 18
                },
                "type": 10,
            },
        ],
    },
    "error_code": 0,
    "error_msg": "",
    "success": true
};


var productMap = {};
var productList=[];


$.each(json.data.secProducts,function(i,item){
    var type=item.type;
    if(!item.product_promotion){// 判断是否有促销
        item.product_promotion={};
    }
    //放入MAP中
    if(typeof productMap[type]=="undefined"){
       productMap[type]=[];
       productMap[type].push(item);
        
    }else{
        productMap[type].push(item);
    }
    
})
alert(JSON.stringify(productMap) )    
//转为数组  
/* 
for in  遍历的是一个对象,

*/
for(var key in productMap ){
    productList.push({type:key,item:productMap[key]});
}


//alert(JSON.stringify(productList));

//找出第个组中最大的len

var len=productList.length;
var maxLen=0;
var maxType=0;

for(var i=0; i<len; i++){
    if(maxLen<productList[i].item.length){
        maxLen=productList[i].item.length;//最大数量
        maxType=productList[i].type;//最大数量的type
     }
}

alert(maxLen);
alert(maxType);

</script>

 

posted @ 2016-04-28 16:58  328201736  阅读(739)  评论(0编辑  收藏  举报