个人平时对 JS 数据结构一些常用的处理方法总结,欢迎大家给出更好的思路

之前项目中用到的一些需求使用js操作数据结构的,大体写了下,如有不合理的地方请指正。

把data转换为newdata

根据月份分组,其中num1是同一月份中name=测试1对应的num值之和,num2是同一月份中name=测试2对应的num值之和,如果没有与之对应的name,则num1赋值0或num2赋值0
    var data = [
        {'month' : '1','name' : '测试1', num:1},
        {'month' : '2','name' : '测试2', num:3},
        {'month' : '1','name' : '测试2', num:2},
        {'month' : '6','name' : '测试1', num:1},
        {'month' : '2','name' : '测试1', num:4},
        {'month' : '1','name' : '测试2', num:2}
    ];
     var newdata = [
        {'month' : '1','num1':1,'num2':4},
        {'month' : '2','num1': 4,'num2':3},
        {'month' : '6','num1' : 1,'num2':0}
    ];

    var unique = {};
    var arr = [];
    for (var i = 0; i < data.length; i++) {
        if(!unique[data[i].month]){
            unique[data[i].month] = data[i].month;
        }
    }
    for (var index in unique) {
        var num1 = 0;
        var num2 = 0;
        for (var i = 0; i < data.length; i++) {
            if(index == data[i].month){
                if(data[i].name =='测试1'){
                    num1 = num1+data[i].num;
                }else{
                    num2 = num2+data[i].num;
                }
            }
        }
        arr.push({
            month:index,
            num1:num1,
            num2:num2
        });
    }
    // console.log(arr);

把data转为newdata

根据no和factory分组,list对应的是二维数组,里面是对应分组数据
   var data = [
        {'no' : '1','factory' : '测试1', num:1,price:2,weight:1},
        {'no' : '2','factory' : '测试2', num:3,price:6,weight:2},
        {'no' : '1','factory' : '测试2', num:2,price:8,weight:5},
        {'no' : '6','factory' : '测试1', num:1,price:5,weight:7},
        {'no' : '2','factory' : '测试1', num:4,price:3,weight:9},
        {'no' : '1','factory' : '测试2', num:2,price:2,weight:3}
    ];

    var newdata = [
        {'no' : '1', 'factory':'测试1','list':[{'num':1,'price':2,'weight':1}]},
        {'no' : '1', 'factory':'测试2','list':[{'num':2,'price':8,'weight':5},{'num':2,'price':2,'weight':3}]},
        {'no' : '2', 'factory':'测试1','list':[{'num':4,'price':3,'weight':9}]},
        {'no' : '2', 'factory':'测试2','list':[{'num':3,'price':6,'weight':2}]},
        {'no' : '6', 'factory':'测试1','list':[{'num':1,'price':5,'weight':7}]}
    ];
    var obj = {};
    var tmp = [];
    for (var i = 0; i < data.length; i++) {
        var des = [];
        if(!obj[data[i].no+data[i].factory]){
            des.push({
                num:data[i].num,
                price:data[i].price,
                weight:data[i].weight
            });
            obj[data[i].no+data[i].factory] = 1;
            tmp.push({
                no:data[i].no,
                factory:data[i].factory,
                list:des
            });
        }else{
            for (var j = 0; j < tmp.length; j++) {
                if(data[i].no == tmp[j].no && data[i].factory == tmp[j].factory){
                    tmp[j]['list'].push({
                        num:data[i].num,
                        price:data[i].price,
                        weight:data[i].weight
                    });
                }
            }
        }
    }
    console.log(newdata);

将data转换成newdata 排序 按键值从大到小排序

    var data = [
        {'no' : 3, 'no1' :2,'no3' :4,'no4' :3},
        {'no' : 9, 'no1' :2,'no3' :7,'no4' :4},
        {'no' : 1, 'no1' :4,'no3' :8,'no4' :1}

    ];
    var newdata = [
        {'no3' : 4, 'no4' :3,'no' :3,'no1' :2},
        {'no' : 9, 'no3' :7,'no4' :4,'no1' :2},
        {'no3' : 8, 'no1' :4,'no' :1,'no4' :1},

    ];
    var newdata = [];
    for (var i = 0; i < data.length; i++) {
        var noArr = [];
        var des = {};
        for (var index in data[i]) {
            noArr.push({
                key:index,
                value:data[i][index]
            }); 
        }

        for (var j = 0; j <noArr.length ; j++) {
            for (var f = 0; f < noArr.length-j-1; f++) {
                if(noArr[f].value<noArr[f+1].value){
                    var swap = noArr[f].value;
                    var key1 = noArr[f].key;
                    noArr[f].value = noArr[f+1].value;
                    noArr[f].key = noArr[f+1].key;
                    noArr[f+1].value = swap;
                    noArr[f+1].key = key1;  
                }
            }
        }
        for (var m = 0; m < noArr.length; m++) {
           var key = noArr[m].key;
           var value = noArr[m].value;
           des[key] = value;
       }
       newdata.push(des);
    }

console.log(newdata);

判断arr的type最后一个的加上flag:end,变成 arr2的形式

        var arr = [
                    {'type':'缝制','id':1},
                    {'type':'缝制','id':2},
                    {'type':'裁剪','id':3},
                    {'type':'厚道','id':4},
                    {'type':'厚道','id':5}
            ];
        var arr2 = [
                    {'type':'缝制','id':1,flag:'notEnd'},
                    {'type':'缝制','id':2,flag:'end'},
                    {'type':'裁剪','id':3,flag:'end'},
                    {'type':'厚道','id':4,flag:'notEnd'},
                    {'type':'厚道','id':5,flag:'end'}
            ];

        var type = '';
        for (var i = 0; i < arr.length; i++) {
            arr[i]['flag'] = 'notEnd';
            if(type != '' && arr[i]['type']!=type){
                arr[i-1]['flag'] = 'end';
            }
            type = arr[i]['type'];

        }
        //最后一个肯定是类型的最后的一个
        arr[i-1]['flag'] = 'end';

        console.log(arr);

将data按多条件升序排序,不使用冒泡,这个各位大佬分享个思路

    var jsonStudents = [
            {name:"Dawson", totalScore:"197", Chinese:"100",math:"97"},
            {name:"HanMeiMei", totalScore:"196",Chinese:"99", math:"97"},
            {name:"HanMeiMei", totalScore:"196",Chinese:"99", math:"10"},
            {name:"HanMeiMei", totalScore:"196",Chinese:"99", math:"80"},
            {name:"LiLei", totalScore:"185", Chinese:"88", math:"97"},
            {name:"XiaoMing", totalScore:"196", Chinese:"96",math:"100"},
            {name:"Jim", totalScore:"196", Chinese:"98",math:"98"},
            {name:"Joy", totalScore:"198", Chinese:"99",math:"99"}
        ];
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 2

干货 收藏了 ,给了不少思路

4年前 评论
张无忌

@ryanxiho 大体总结了一下,不是很完善

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!