1. 程式人生 > >對資料處理

對資料處理

var arr = [
    { name: 1, id: 1, queue: 2 },
    { name: 2, id: 6, queue: 9 },
    { name: 3, id: 4, queue: 8 },
    { name: 4, id: 5, queue: 7 },
    { name: 5, id: 0, queue: 6 },
    { name: 6, id: 6, queue: 5 },
    { name: 6, id: 6, queue: 0 },
    { name: 7, id: 2, queue: 24 },
    { name: 8, id: 3, queue: 3 },
    { name: 9, id: 0, queue: 1 },
    { name: 10, id: 2, queue: 0 },
    { name: 11, id: 4, queue: 22 },
    { name: 11, id: 8, queue: 12 },
];
var order = [0, 6, 1, 2, 3, 4, 5];
var newarr = [];

for (var i = 0; i < order.length; i++) {
    for (var j = 0; j < arr.length; j++) {
        if (arr[j].id === order[i]) {
            newarr.push(arr[j]);
            arr.splice(j, 1);
            j--;

        }
    }
}
console.log(newarr.concat(arr))

newarr = newarr.concat(arr);
console.log(newarr);
var newnewarr = [];
var obj = {};
var j = 0;
newnewarr[j] = [];
for (var i = 0; i < newarr.length; i++) {
    obj[newarr[0].id] = true;
    if (!obj[newarr[i].id]) {
        j++;
        newnewarr[j] = [];
        obj[newarr[i].id] = true;
    }
    newnewarr[j].push(newarr[i])
}
console.log(newnewarr);

function sort(arr) {
    arr.sort(function(a, b) {
        return b.queue - a.queue;
    });
}
for (var i = 0; i < newnewarr.length; i++) {
    sort(newnewarr[i])
}
//現在的newnewarr就是需要的陣列
var resultArr = [];
newnewarr.forEach(item => {
    console.log(item)
    resultArr = resultArr.concat(item)
})
console.log(resultArr);

將資料先按照 order = [0, 6, 1, 2, 3, 4, 5],這個順序排列

在按照 queue 從小到大排列