使用 javaScript for 循环对数组进行排序时遇到问题

时间:2021-07-19 06:17:20

标签: javascript arrays for-loop

我遇到以下问题,但还没有弄清楚原因。 我只是想将硬编码版本写入具有相同迭代次数的 for 循环。

硬编码以供参考:

list = []
list.push(addSpace(test)) //0
test.shift()
list.push(addSpace(test)) //1
test.shift()
list.push(addSpace(test)) //2
test.shift()
list.push(addSpace(test)) //3
test.shift()
list.push(addSpace(test)) //4
test.shift()
list.push(addSpace(test)) //5
test.shift()
list.push(addSpace(test)) //6
test.shift()
list.push(addSpace(test)) //7
test.shift()
list.push(addSpace(test)) //8
let merged = [].concat.apply([], list);
console.log(merged);
console.log(`# of items in merged: ${merged.length}`)

输出:

[
  [ 'C', '', '', '', '' ], [ 'C', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ 'C', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ '', 'D', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ 'C', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ 'C', '', '', '', '' ],
  [ '', '', '', '', '' ],  [ '', '', '', '', 'G' ],
  [ '', '', '', '', 'G' ], [ '', '', '', '', '' ],
  [ '', '', 'E', '', '' ], [ '', '', '', '', '' ],
  [ '', 'D', '', '', '' ], [ 'C', '', '', '', '' ],
  [ 'C', '', '', '', '' ]
]
# of items in merged: 41

我试图将上述内容放入循环中,但为什么我的循环没有完成?

function prepList(input) {
    let list = [];
    for (let i = 0; i < input.length; i++) {
        list.push(addSpace(input))
        test.shift()
    }
    list.push(addSpace(input))
    let merged = [].concat.apply([], list);
    return merged
}

console.log(prepList(test))

输出:

[
  [ 'C', '', '', '', '' ], [ 'C', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ 'C', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ '', 'D', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ 'C', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ],
  [ 'C', '', '', '', '' ], [ '', '', '', '', '' ]
]
# of items in merged: 30

使用的数组:

let test= [
  [ 'C', 'C', 'C', '', 'C' ],
  [ '', 'C', '', 'C', '' ],
  [ 'C', 'C', 'C', '', 'C' ],
  [ '', 'C', '', 'D', '' ],
  [ 'C', 'C', 'C', '', 'C' ],
  [ '', 'C', '', 'C', '' ],
  [ 'C', 'C', '', 'G', 'G' ],
  [ '', 'E', '', 'D', 'C' ],
  [ 'C' ]
]

使用的addSpace() 函数:

function addSpace(arr) {
    let newList = []
    for (let i = 0; i <= 5; i++) {
        for (let j = 0; j <= 5; j++) {
            // to refresh the x everytime as splice will alter original x
            let x = ['', '', '', '']
            if (arr[i][j] == 'C') {
                x.splice(0, 0, arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == 'D') {
                x.splice(1, 0, arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == 'E') {
                x.splice(2, 0, arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == 'F') {
                x.splice(3, 0, arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == 'G') {
                x.splice(4, 0, arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == '') {
                x.push('')
                newList.push(x)
            }
        }
        return newList
    }
}

1 个答案:

答案 0 :(得分:0)

保存 list.length 属性,因为您使用 test.shift() 对其进行了变异!

const length = test.length; // this was added
for (let i=0; i<length; i++) {
  list.push(addSpace(test))
  test.shift()
}

let test = [
  ['C', 'C', 'C', '', 'C'],
  ['', 'C', '', 'C', ''],
  ['C', 'C', 'C', '', 'C'],
  ['', 'C', '', 'D', ''],
  ['C', 'C', 'C', '', 'C'],
  ['', 'C', '', 'C', ''],
  ['C', 'C', '', 'G', 'G'],
  ['', 'E', '', 'D', 'C'],
  ['C']
]
list = []
const length = test.length; // this was added
for (let i=0; i<length; i++) {
  list.push(addSpace(test))
  test.shift()
}


let merged = [].concat.apply([], list);
console.log(merged);
console.log(`# of items in merged: ${merged.length}`)

function addSpace(arr) {
  let newList = []
  for (let i = 0; i <= 5; i++) {
    for (let j = 0; j <= 5; j++) {
      // to refresh the x everytime as splice will alter original x
      let x = ['', '', '', '']
      if (arr[i][j] == 'C') {
        x.splice(0, 0, arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == 'D') {
        x.splice(1, 0, arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == 'E') {
        x.splice(2, 0, arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == 'F') {
        x.splice(3, 0, arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == 'G') {
        x.splice(4, 0, arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == '') {
        x.push('')
        newList.push(x)
      }
    }
    return newList
  }
}