亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對數組內的數組進行排序時出現問題:這里創建了一個未定義的數組元素

對數組內的數組進行排序時出現問題:這里創建了一個未定義的數組元素

湖上湖 2023-07-20 14:53:55
因此,我一直在為學校內的一家商店制作一個網站,以獲得一些經驗,也許以后會成為一名網絡開發人員。網站后端編程比我預想的要有趣得多,直到我遇到這個問題,我已經調試了幾個小時,但進展甚微。我用一個稍微簡單的函數重新創建了這種情況。請允許我解釋一下一切的目的:arrayMain:我要排序的數組。它包含更多我希望按 1 位置中的數字排序的數組。arrayBase:該副本的arrayMain目的是永遠不會被編輯。如果需要,主要用于“取消排序” arrayMain。arrayTemp:它的副本arrayBase用于選擇排序。重復地將編號最小的項目移回arrayMain。min和transfer:要移動 1 個數組元素,則arrayTemp掃描整個數組。min從列表中最后一項的數字開始。如果檢測到新的最小值,則將其設置為transfer。當檢查完所有項目后,arrayTemp[transfer]被移入arrayMain。var arrayMain = [["a", 15, "c"], ["a", 18, "c"], ["a", 11, "c"] ,["a", 15, "c"], ["a", 25, "c"]]var arrayBase = arrayMain.slice(0)testFunc = function() {  // Clear the main array and prepare to rebuild it in order  arrayMain = []  let arrayTemp = arrayBase.slice(0)  // Length of the array times, find the element with smallest number in position 1 and move it over to arrayMain.  // This is supposed to ignore the last element since that one does not require calculations  for (let i = arrayTemp.length; i > 0; i--) {    let min = arrayTemp[arrayTemp.length - 1][1], transfer = arrayTemp.length - 1    for (let x = (arrayTemp.length - 2); x >= 0; x--) {      if (arrayTemp[x][1] >= min) {        min = arrayTemp[x][1]        transfer = x      }    }    arrayMain.unshift(arrayTemp[transfer])    arrayTemp.splice(transfer, 1)  }  // Move over the last array element and log the results  arrayMain.unshift(arrayTemp[0])  console.log(arrayMain)}testFunc()預期結果:[ ["a", 11, "c"], ["a", 15, "c"], ["a", 15, "c"], ["a", 18, "c"], ["a", 25, "c"] ]實際結果:[ undefined, ["a", 11, "c"], ["a", 15, "c"], ["a", 15, "c"], ["a", 18, "c"], ["a", 25, "c"] ]我知道刪除它很容易arrayMain[0],但我想知道是否有一種方法可以阻止這個未定義的元素首先出現,或者至少知道是什么造成了它。或者,如果你有自己的排序方式,不僅有效而且速度更快,我想我也會接受這一點,但我真的想知道它來自哪里,因為undefined如果我從來沒有弄清楚我將來可能會再次陷入這種境地。
查看完整描述

2 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

for (let i = arrayTemp.length; i > 0; i--) {

當您在循環后插入最后一個元素時,您的循環次數太多了。

for (let i = arrayTemp.length - 1; i > 0; i--) {

一種更具邏輯性和可讀性的方式可能是:

while (arrayTemp.length > 1)
{    
    // your logic here
}


查看完整回答
反對 回復 2023-07-20
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

您在外循環中循環所有項目,并且也取消移動最后一個項目。


const

    testFunc = function() {

        // Clear the main array and prepare to rebuild it in order

        arrayMain = [];

        let arrayTemp = arrayBase.slice(0)


        // Length of the array times, find the element with smallest number

        // in position 1 and move it over to arrayMain.

        // This is supposed to ignore the last element since that one does not

        // require calculations

        for (let i = arrayTemp.length; i > 0; i--) {

            let min = arrayTemp[arrayTemp.length - 1][1],

                transfer = arrayTemp.length - 1;

                

            for (let x = arrayTemp.length - 2; x >= 0; x--) {

                if (arrayTemp[x][1] >= min) {

                    min = arrayTemp[x][1];

                    transfer = x;

                }

            }

            arrayMain.unshift(arrayTemp[transfer])

            arrayTemp.splice(transfer, 1)

        }


        // Move over the last array element and log the results

 //           arrayMain.unshift(arrayTemp[0])

        console.log(arrayMain);

    };



var arrayMain = [["a", 15, "c"], ["a", 18, "c"], ["a", 11, "c"], ["a", 15, "c"], ["a", 25, "c"]]

var arrayBase = arrayMain.slice(0)


testFunc();

.as-console-wrapper { max-height: 100% !important; top: 0; }

length - 1為了解決這個問題,您需要使用和 take作為索引來調整起始值,i而不是使用length.


const

    testFunc = function() {

        let arrayMain = [];

        let arrayTemp = arrayBase.slice(0),

            i = arrayTemp.length;


        while (--i) {

            let min = arrayTemp[i][1],

                transfer = arrayTemp.length - 1,

                x = i;

                

            while (x--) {

                if (arrayTemp[x][1] >= min) {

                    min = arrayTemp[x][1];

                    transfer = x;

                }

            }

            arrayMain.unshift(arrayTemp[transfer])

            arrayTemp.splice(transfer, 1)

        }


        arrayMain.unshift(arrayTemp[0])

        console.log(arrayMain);

    };



var arrayMain = [["a", 15, "c"], ["a", 18, "c"], ["a", 11, "c"], ["a", 15, "c"], ["a", 25, "c"]]

var arrayBase = arrayMain.slice(0)


testFunc();

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對 回復 2023-07-20
  • 2 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號