본문 바로가기
Programming/JSP

json 배열 합치기(merge) 및 소팅하기

by Chan_찬 2016. 1. 13.
728x90
var by = function (name) {
return function (o, p) {
var a, b;
if (typeof o === 'object' && typeof p === 'object' && o && p) {
a = o[name];
b = p[name];
if (a === b) {
return 0;
}
if (typeof a === typeof b) {
return a > b ? -1 : 1;
}
return typeof a > typeof b ? -1 : 1;
} else {
throw {
name: 'Error',
message: 'Expected an object when sorting by ' + name
};
}
};
};

alerts2 = [{
"id": "test",
"commandNo": "1",
"insertDt": "2016-01-12 11:15:48.0",
"updateDt": "2016-01-08 16:35:49.0"
}, {
"id": "test1",
"commandNo": "2",
"insertDt": "2016-01-11 13:15:48.0",
"updateDt": "2016-01-08 16:35:49.0"
}, {
"id": "test2",
"commandNo": "3",
"insertDt": "2016-01-06 16:35:48.0",
"updateDt": "2016-01-08 16:35:49.0"
}];
alerts = ${alertList}; //alertList는 json 배열
$.merge(alerts, alerts2); // 두개의 json 배열 하나로 합치기

// alerts = alerts.concat(alerts2); //위 merge와 concat 둘다 가능

alerts.sort(by('insertDt')); //위 함수로 소팅하기


728x90
728x90
Buy me a coffeeBuy me a coffee

댓글