第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

對數(shù)組內(nèi)的數(shù)組進(jìn)行排序時(shí)出現(xiàn)問題:這里創(chuàng)建了一個(gè)未定義的數(shù)組元素

對數(shù)組內(nèi)的數(shù)組進(jìn)行排序時(shí)出現(xiàn)問題:這里創(chuàng)建了一個(gè)未定義的數(shù)組元素

湖上湖 2023-07-20 14:53:55
因此,我一直在為學(xué)校內(nèi)的一家商店制作一個(gè)網(wǎng)站,以獲得一些經(jīng)驗(yàn),也許以后會成為一名網(wǎng)絡(luò)開發(fā)人員。網(wǎng)站后端編程比我預(yù)想的要有趣得多,直到我遇到這個(gè)問題,我已經(jīng)調(diào)試了幾個(gè)小時(shí),但進(jìn)展甚微。我用一個(gè)稍微簡單的函數(shù)重新創(chuàng)建了這種情況。請?jiān)试S我解釋一下一切的目的:arrayMain:我要排序的數(shù)組。它包含更多我希望按 1 位置中的數(shù)字排序的數(shù)組。arrayBase:該副本的arrayMain目的是永遠(yuǎn)不會被編輯。如果需要,主要用于“取消排序” arrayMain。arrayTemp:它的副本arrayBase用于選擇排序。重復(fù)地將編號最小的項(xiàng)目移回arrayMain。min和transfer:要移動 1 個(gè)數(shù)組元素,則arrayTemp掃描整個(gè)數(shù)組。min從列表中最后一項(xiàng)的數(shù)字開始。如果檢測到新的最小值,則將其設(shè)置為transfer。當(dāng)檢查完所有項(xiàng)目后,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()預(yù)期結(jié)果:[ ["a", 11, "c"], ["a", 15, "c"], ["a", 15, "c"], ["a", 18, "c"], ["a", 25, "c"] ]實(shí)際結(jié)果:[ undefined, ["a", 11, "c"], ["a", 15, "c"], ["a", 15, "c"], ["a", 18, "c"], ["a", 25, "c"] ]我知道刪除它很容易arrayMain[0],但我想知道是否有一種方法可以阻止這個(gè)未定義的元素首先出現(xiàn),或者至少知道是什么造成了它?;蛘?,如果你有自己的排序方式,不僅有效而且速度更快,我想我也會接受這一點(diǎn),但我真的想知道它來自哪里,因?yàn)閡ndefined如果我從來沒有弄清楚我將來可能會再次陷入這種境地。
查看完整描述

2 回答

?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

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

當(dāng)您在循環(huán)后插入最后一個(gè)元素時(shí),您的循環(huán)次數(shù)太多了。

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

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

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


查看完整回答
反對 回復(fù) 2023-07-20
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

您在外循環(huán)中循環(huán)所有項(xiàng)目,并且也取消移動最后一個(gè)項(xiàng)目。


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為了解決這個(gè)問題,您需要使用和 take作為索引來調(diào)整起始值,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; }


查看完整回答
反對 回復(fù) 2023-07-20
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號