根据第一维中的某些项目排列二维数组

时间:2018-12-30 01:31:49

标签: javascript

我有一个二维数组。并非每个维度中的所有项目都相似,但是我想确保第二个数组中的项目与第一个相同。

例如:

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"]
]

有什么建议吗?

注意重要的是保留名称在第一个数组中的原始位置,而不仅仅是将所有内容排在最后

4 个答案:

答案 0 :(得分:1)

数组有一个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])

答案 1 :(得分:0)

您可以获得第一个数组并将其存储在变量(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 :(得分:0)

如果部仅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)

输出应为

[[['杰西卡(Jessica),'阿什利(Ashley)','艾米丽(Emily)','萨曼莎(Samantha)','萨拉(Sarah),'泰勒(Taylor)'],   ['Hannah','Brittany','Amanda','Samantha','Sarah','Taylor']]

答案 3 :(得分:0)

                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)

尝试一下