格式:
var cases = [{"id":"http://wb.gd.labscare.com/lab/#/public/1011462120167579094","t_code":"B01-R10QY04-001","t_code2":"B01-R10QY04-018","id2":"http://jthg.gd.labscare.com/lab/#/public/1011463417778803040"},{"id":"http://wb.gd.labscare.com/lab/#/public/1011467465864884014","t_code":"B01-R10QY04-005","t_code2":"","id2":""}]
var objData = []
function t_tamiStorage() {
return {
id: "",
t_code: "",
t_code2: "",
id2: ""
}
}
代码
//最终结果结构体
var idx = 0;
for (var i = 0; i < cases.length; i++) {
var item = cases[i]
if(i%2===0){
var showdata = t_tamiStorage();
objData.push(showdata);
showdata.t_code = item['1066989928805654067'];
showdata.id = getUrl + item.id;
idx++;
}else{
objData[idx-1]['t_code2'] = item['1066989928805654067'];
objData[idx-1]['id2'] = getUrl + item.id;
}
}
objData.sort((a, b) => {
// 比较t_code
const tCodeComparison = a.t_code.localeCompare(b.t_code);
if (tCodeComparison !== 0) return tCodeComparison;
return a.t_code2.localeCompare(b.t_code2);
});
console.log(objData)
打印
t_code,t_code2是标识符,
请问大佬们这个要排序(升序)呢
还是一样的直接用 Array.sort() 这个API做排序就行了。