2015-06-02 54 views
1

我最近張貼了這個問題的PHP,但現在我希望它爲JavaScript我要排序3維數組在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是陣列

+1

這根本不是一個數組,更不用說一個3D - 它只是一個對象,對象的屬性沒有對它們的順序。 – dave

+1

我們不是代碼翻譯服務... –

+0

你不能那樣做... JavaScript對象是無序的。 – tymeJV

回答

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 
+0

它沒有工作...我想'inboxMessages = {378552:{0:{...},1:{...}},224199:{0:{...},1:{... {0:{0:{...},1:{...}},1:{0:{...},1:{...} }}}'它的排序完美,但'thread_id'不存在爲鍵。 – kupendra

+0

在JavaScript中,對象(使用大括號{})的鍵沒有固定的順序。您可以*按照特定順序*創建*一個對象,但不能確定它們將按照該順序*存儲*。爲了保持順序,你需要使用一個數組(使用方括號[])。一種解決方案是創建一個'thread_id'的有序數組,並使用它來在嵌套'output'數組中找到合適的對象。 –

+0

我已經添加了幾個方法,以便您可以通過thread_id找到給定線程的數據。 –

0

這個數組已經從PHP服務器腳本排序但是,當我從阿賈克斯得到它,按升序轉換JSON到數組則只有Chrome網絡瀏覽器來排序的數字thread_id

所以我現在加_之前thread_id從服務器端到關鍵_378552現在是字符串,所以沒有按CHROME排序。

而畢竟我刪除_thread_id而訪問它..所以我的問題解決了。這只是Chrome瀏覽器中的問題。