簡體   English   中英

根據第一維中的某些項目排列二維數組

[英]Arrange a two dimensional array based on some of the items in the first dimension

我有一個二維數組。 並非每個維度中的所有項目都相似,但是我想確保第二個數組中的項目與第一個相同。

因此,例如:

array = [
    ["Jessica", "Ashley", "Emily", "Samantha", "Sarah", "Taylor"]
    ["Samantha", "Sarah", "Taylor", "Hannah", "Brittany", "Amanda"]
]

應該變成:

array = [
   ["Jessica", "Ashley", "Emily", "Samantha", "Sarah", "Taylor"]
   ["Hannah", "Brittany", "Amanda","Samantha", "Sarah", "Taylor"]
]

有什么建議么?

注意, 重要的是保留名稱在第一個數組中的原始位置,而不僅僅是將所有內容排序到最后

數組具有帶函數的sort()方法。 在該函數中,您可以在第一個數組中使用indexOf查找名稱的索引並對其進行排序:

 let arr = [ ["Jessica", "Ashley", "Emily", "Samantha", "Sarah", "Taylor"], ["Samantha", "Sarah", "Taylor", "Hannah", "Brittany", "Amanda"] ] arr[1].sort((a, b) => arr[0].indexOf(a) - arr[0].indexOf(b)) console.log(arr[1]) 

這不是很有效-它必須多次瀏覽第一個數組才能對第二個數組進行排序。 如果您有很多值並且第一個版本成為瓶頸,則可以創建一個將鍵映射到第一個數組的索引的查找:

 let arr = [ ["Jessica", "Ashley", "Emily", "Samantha", "Sarah", "Taylor"], ["Samantha", "Sarah", "Taylor", "Hannah", "Brittany", "Amanda"] ] let lookup = arr[0].reduce((lookup, name, index) => { lookup[name] = index return lookup }, {}) // when name is not in lookup use -1 to make them sort first arr[1].sort((a, b) => (lookup[a] || -1) - (lookup[b] || -1)) console.log(arr[1]) 

編輯

為了保持相同的索引而不是相同的排序順序,可以遍歷第一個數組並交換第二個數組的位置。 這樣可以確保您始終先將較早的物品放在正確的位置,這樣就不會重新交換它們:

 let arr = [["Jessica", "Ashley", "Emily", "Samantha", "Sarah", "Taylor"], ["Hannah", "Brittany", "Amanda", "Ashley", "Samantha", "Taylor"]] arr[0].forEach((item , i)=> { let index = arr[1].indexOf(item); if (index >= 0){ [arr[1][i], arr[1][index]] = [arr[1][index], arr[1][i]] } }) console.log(arr[1]) 

您可以獲取第一個數組並將其存儲在變量中( let first_arr = arr[0] )。 然后,您可以遍歷所有內部數組,並使用first_array.indexOf()檢查給定數組的索引是否與第一個數組中的索引匹配。 如果不匹配,則需要交換兩個值。

請參見下面的工作示例(閱讀代碼注釋以獲取解釋):

 let arr = [["Jessica", "Ashley", "Emily", "Samantha", "Sarah", "Taylor"], ["Samantha", "Sarah", "Taylor", "Hannah", "Brittany", "Amanda"]]; let first_arr = arr[0]; // get the first array as the "base" array for (let i = 1; i < arr.length; i++) { // loop over the 2d array let current_arr = arr[i]; // get the current array and store in in a variable for (let j = 0; j < current_arr.length; j++) { // loop over each index in the current array let name = current_arr[j]; // get the name at a given index in the array let nameIndex = first_arr.indexOf(name); // get the index of the name in the first array if (nameIndex > -1 && nameIndex != j) { // check if the name is in the correct position already. (Also if nameIndex == -1 then it doesn't exsist in the original array) // If the name isn't in the correct index position then SWAP the values around: let tmp = current_arr[j]; current_arr[j] = current_arr[nameIndex]; current_arr[nameIndex] = tmp; } } } console.log(arr); // log the modified array 

如果部門僅為2,則不需要兩個循環。

 let array = [ ["Jessica", "Ashley", "Emily", "Samantha", "Sarah", "Taylor"], ["Samantha", "Sarah", "Taylor", "Hannah", "Brittany", "Amanda"] ]; /** Loop through the inner array */ for (let i in array[1]) { /** Check to see if the outer array contains the value */ let index = array[0].indexOf(array[1][i]) /** If it does swap it's location */ if (index > -1) { let temp = array[1][index]; array[1][index] = array[1][i] array[1][i] = temp } } console.log(array) 

輸出應為

[] ']]

                array = [
                    ["Jessica", "Ashley", "Emily", "Samantha", "Sarah", "Taylor"],
                    ["Samantha", "Sarah", "Taylor", "Hannah", "Brittany", "Amanda"]
                ]

                size = array.length
                size1 = array[0].length

                document.write(size1);


                for(var i=0; i<size1; i++){
                  for(var j=0; j<size1; j++){
                    if(array[0][i] == array[1][j] ){
                      var x = array[1][j]
                      var y = array[1][i]

                      array[1][i] = x
                      array[1][j] = y
                    }
                  }
                }

                console.log(array)

試試看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM