個人平時對 JS 資料結構一些常用的處理方法總結,歡迎大家給出更好的思路

張無忌發表於2019-07-23

之前專案中用到的一些需求使用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 協議》,轉載必須註明作者和本文連結

相關文章