我最近发布了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
线程数组在数组
答案 0 :(得分:1)
这样做你想要的吗?
//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
答案 1 :(得分:0)
此数组已经从PHP的服务器脚本进行排序但是当我从ajax获取它并将JSON转换为数组时,只有chrome web浏览器按数字thread_id
按升序对其进行排序。
所以现在我在_
之前从服务器端添加thread_id
到密钥,因为_378552
现在是字符串所以没有按照CHROME排序。
毕竟我在访问_
时删除了thread_id
..所以我的问题解决了。这只是Chrome浏览器中的问题。