程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 javaScript for 循环对数组进行排序时遇到问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 javaScript for 循环对数组进行排序时遇到问题?

开发过程中遇到使用 javaScript for 循环对数组进行排序时遇到问题的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 javaScript for 循环对数组进行排序时遇到问题的解决方法建议,希望对你解决使用 javaScript for 循环对数组进行排序时遇到问题有所启发或帮助;

我遇到以下问题,但还没有弄清楚原因。 我只是想将硬编码版本写入具有相同迭代次数的 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',[ '','D','G' ],'E','' ]
]
# 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','' ]
]
# of items in merged: 30

使用的数组:

let test= [
  [ 'C','C','C' ],'G',[ '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,arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == 'D') {
                x.splice(1,arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == 'E') {
                x.splice(2,arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == 'F') {
                x.splice(3,arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == 'G') {
                x.splice(4,arr[i][j])
                newList.push(x)
            } else if (arr[i][j] == '') {
                x.push('')
                newList.push(x)
            }
        }
        return newList
    }
}

解决方法

保存 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','D','G','G'],'E',['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,arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == 'D') {
        x.splice(1,arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == 'E') {
        x.splice(2,arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == 'F') {
        x.splice(3,arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == 'G') {
        x.splice(4,arr[i][j])
        newList.push(x)
      } else if (arr[i][j] == '') {
        x.push('')
        newList.push(x)
      }
    }
    return newList
  }
}

大佬总结

以上是大佬教程为你收集整理的使用 javaScript for 循环对数组进行排序时遇到问题全部内容,希望文章能够帮你解决使用 javaScript for 循环对数组进行排序时遇到问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: