繁体   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