[英]I want to sort 3 dimensional array in javascript
我最近針對PHP發布了這個問題,但現在我希望將其用於javascript
我的數組如下:
var inboxMessages = {
105775: { //index is thread_id
0: {
'id': 85,
'thread_id': 105775,
'message': "hello",
'created_at': "May 20, 2015",
'datetime': 1432118191,
'sender_id': 13,
},
1: {
'id': 70,
'thread_id': 105775,
'message': "hii",
'created_at': "May 19, 2015",
'datetime': 1432021227,
'sender_id': 13,
}
},
224199: { //index is thread_id
0: {
'id': 88,
'thread_id': 224199,
'message': "yessss...",
'created_at': "May 20, 2015",
'datetime': 1432306513,
'sender_id': 14,
},
1: {
'id': 75,
'thread_id': 224199,
'message': "hellowwww...",
'created_at': "May 19, 2015",
'datetime': 1432021227,
'sender_id': 14,
}
},
107917: { //index is thread_id
0: {
'id': 56,
'thread_id': 107917,
'message': "how r u??",
'created_at': "May 16, 2015",
'datetime': 1431792155,
'sender_id': 14,
},
1: {
'id': 30,
'thread_id': 107917,
'message': "hi.. i m fine.",
'created_at': "May 6, 2015",
'datetime': 1430920006,
'sender_id': 14,
},
2: {
'id': 30,
'thread_id': 107917,
'message': "hi!!!!..how r u??",
'created_at': "May 6, 2015",
'datetime': 1430920006,
'sender_id': 14,
}
},
378552: { //index is thread_id
0: {
'id': 108,
'thread_id': 378552,
'message': "hi",
'created_at': "May 29, 2015",
'datetime': 1432906923,
'sender_id': 14,
},
1: {
'id': 107,
'thread_id': 378552,
'message': "hi.",
'created_at': "May 29, 2015",
'datetime': 1432903194,
'sender_id': 14,
}
}
};
所以現在我需要這樣的輸出:
var inboxMessages = {
378552: {//index is thread_id
0: {
'id': 108,
'thread_id': 378552,
'message': "hi",
'created_at': "May 29, 2015",
'datetime': 1432906923,
'sender_id': 14,
},
1: {
'id': 107,
'thread_id': 378552,
'message': "hi.",
'created_at': "May 29, 2015",
'datetime': 1432903194,
'sender_id': 14,
}
},
224199: {//index is thread_id
0: {
'id': 88,
'thread_id': 224199,
'message': "yessss...",
'created_at': "May 20, 2015",
'datetime': 1432306513,
'sender_id': 14,
},
1: {//index is thread_id
'id': 75,
'thread_id': 224199,
'message': "hellowwww...",
'created_at': "May 19, 2015",
'datetime': 1432021227,
'sender_id': 14,
}
},
105775: {//index is thread_id
0: {
'id': 85,
'thread_id': 105775,
'message': "hello",
'created_at': "May 20, 2015",
'datetime': 1432118191,
'sender_id': 13,
},
1: {
'id': 70,
'thread_id': 105775,
'message': "hii",
'created_at': "May 19, 2015",
'datetime': 1432021227,
'sender_id': 13,
}
},
107917: {//index is thread_id
0: {
'id': 56,
'thread_id': 107917,
'message': "how r u??",
'created_at': "May 16, 2015",
'datetime': 1431792155,
'sender_id': 14,
},
1: {
'id': 30,
'thread_id': 107917,
'message': "hi.. i m fine.",
'created_at': "May 6, 2015",
'datetime': 1430920006,
'sender_id': 14,
},
2: {
'id': 30,
'thread_id': 107917,
'message': "hi!!!!..how r u??",
'created_at': "May 6, 2015",
'datetime': 1430920006,
'sender_id': 14,
}
},
};
我不知道如何在JS中排序。
我需要線程數組的最近datetime
時間在數組頂部
這是您想要的嗎?
//var inboxMessages = {... your input ...};
function convertToOrderedArrays(inboxMessages) {
var output = [];
var thread_ids = Object.keys(inboxMessages);
var threadObject, threadArray, keys;
for (var ii=0, thread_id; thread_id=thread_ids[ii]; ii++) {
threadObject = inboxMessages[thread_id];
keys = Object.keys(threadObject);
threadArray = [];
output.push(threadArray);
for (var jj=0, key; key=keys[jj]; jj++) {
threadArray.push(threadObject[key]);
}
threadArray.sort(function (a, b) {
return (b.datetime - a.datetime);
});
}
output.sort(function (a, b) {
return(b[0].datetime - a[0].datetime);
});
return output;
}
function getThreadIdArray(threadArrays) {
var thread_id_array = [];
for (var ii=0, thread_array; thread_array=threadArrays[ii]; ii++) {
thread_id = thread_array[0].thread_id;
thread_id_array.push(thread_id);
}
return thread_id_array;
}
function findThreadArray(thread_id, thread_id_array, threadArrays) {
var index = thread_id_array.indexOf(thread_id);
var threadArray;
if (index < 0) {
// no matching thread was found
} else {
threadArray = threadArrays[index];
}
return threadArray;
}
var output = convertToOrderedArrays(inboxMessages);
var thread_id_array = getThreadIdArray(output);
var mostRecentThread = thread_id_array[0];
var mostRecentMessage = output[0][0];
var thread105775 = findThreadArray(105775, thread_id_array, output);
console.log(output);
// sorted array of sorted arrays
console.log(thread_id_array);
// [378552, 224199, 105775, 107917]
console.log(mostRecentThread);
// 378552
console.log(mostRecentMessage);
/* {
created_at: "May 29, 2015"
, datetime: 1432906923
, id: 108
, message: "hi"
, sender_id: 14
, thread_id: 378552
} */
console.log(thread105775);
// sorted array
該數組已經從PHP的服務器腳本中進行排序,但是當我從ajax獲取並將JSON轉換為數組時,只有chrome瀏覽器將其按數字thread_id
升序排序。
因此,現在我在從服務器_378552
密鑰的thread_id
之前添加_
,因為_378552
現在是字符串,因此無法按CHROME排序。
畢竟我在訪問它時從thread_id
刪除了_
..所以我的問題解決了。 這只是Chrome瀏覽器中的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.