首页 > 解决方案 > 我无法存储中间数组

问题描述

我有一个冒泡排序代码:

      var ani = []
export const Bubblesort =(arr) =>{
    for(var i = 0; i < arr.length; i++){
     
        // Last i elements are already in place  
        for(var j = 0; j < ( arr.length - i -1 ); j++){
            
          // Checking if the item at present iteration 
          // is greater than the next iteration
          if(arr[j] > arr[j+1]){
            // If the condition is true then swap them
            var temp = arr[j]
            arr[j] = arr[j + 1]
            arr[j+1] = temp
          }
          console.log (arr)

        }
      }
      // Print the sorted array
      return arr
}

在此处输入图像描述

在这里,我想在打印数组时获取数组中元素的所有位置,它在我的控制台中显示了多个数组。应该是这样,但是当我将其设为数组组时,所有组数组都具有相同的编号。

更换这个

console.log (arr)

对此

 ani.push(arr)
 console.log(arr)

在此处输入图像描述

这给了我错误的输出。请帮忙.....

标签: javascriptnode.jsarraysreactjsdom-events

解决方案


推荐阅读