簡體   English   中英

為什么刪除數組的舊內容?

[英]Why is the old content of the array deleted?

 let sentenceArr = [['Teachers', 'are', 'clever.'], ['Developers', 'are', 'more', 'clever,', 'because', 'of', 'her', 'practice', 'experience.']]; for(let i=0; i<sentenceArr.length;i++){ console.log('loop: ' + i +1); splitPunctuationMarks(sentenceArr[i]); } function splitPunctuationMarks(inSentence_As_Arr){ let punctuationMarks = ['.', '!', '?', ',', ';', ':']; let containsPunctuationMark = (-1); let splitParts = []; let parts = []; for(let i=0; i < inSentence_As_Arr.length; i++){ for(let j=0; j< punctuationMarks.length; j++){ containsPunctuationMark = inSentence_As_Arr[i].indexOf(punctuationMarks[j]); if(containsPunctuationMark >= 0){ console.log('### splitPunctuationMarks ' + inSentence_As_Arr[i]); console.log('parts 1: '); console.log(parts); parts[0] = i; parts[1] = inSentence_As_Arr[i].slice(0, containsPunctuationMark); parts[2] = inSentence_As_Arr[i].slice(containsPunctuationMark); console.log('parts 2: '); console.log(parts); console.log('splitParts1: '); console.log(splitParts); splitParts.push(parts); console.log('splitParts2: '); console.log(splitParts); containsPunctuationMark = (-1); //parts = []; } } } } 

我編寫了一個功能,該功能以超出預期的方式運行。

我還找到了一種解決方案,可以提供想要的結果。 現在,我想了解一下,我的期望出了什么問題。

那是我的代碼:

let sentenceArr = ['Teachers are clever.', 'Developers are more clever, because of her practice experience.'];

for(let i=0; i<sentenceArr.length;i++){
    console.log('loop: ' + i +1);
    splitPunctuationMarks(sentenceArr[i]);
}

function splitPunctuationMarks(inSentence_As_Arr){
    let punctuationMarks = ['.', '!', '?', ',', ';', ':'];
    let containsPunctuationMark = (-1);
    let splitParts = [];
    let parts = [];

    for(let i=0; i < inSentence_As_Arr.length; i++){
        for(let j=0; j< punctuationMarks.length; j++){
            containsPunctuationMark = inSentence_As_Arr[i].indexOf(punctuationMarks[j]);

            if(containsPunctuationMark >= 0){
                console.log('### splitPunctuationMarks ' + inSentence_As_Arr[i]);
                console.log('parts 1: ');
                console.log(parts);
                parts[0] = i;
                parts[1] = inSentence_As_Arr[i].slice(0, containsPunctuationMark);
                parts[2] = inSentence_As_Arr[i].slice(containsPunctuationMark);
                console.log('parts 2: ');
                console.log(parts);

                console.log('splitParts1: ');
                console.log(splitParts);

                splitParts.push(parts);

                console.log('splitParts2: ');
                console.log(splitParts);

                containsPunctuationMark = (-1);
                //parts = [];
            }
        }
    }
}

我所期望的:

第一個循環:

'### splitPunctuation標記為聰明。

parts1:[] //因為尚未填充空數組。

parts2:[2,'clever','。'] //因為這是第一句中的跟蹤數據。

splitParts1:[] //因為尚未填充空數組。

splitParts2:[2,'clever','。'] //因為這是零件中的數據

第二循環:

'### splitPunctuation標記很聰明,

parts1:[] //因為尚未填充空數組。

parts2:[3,'clever',','] //因為這是第二句中的跟蹤數據。

splitParts1:[] //因為尚未填充空數組。

splitParts2:[3,'clever',','] //因為這是零件中的數據

'### splitPunctuationMarks經驗。

parts1:[3,'clever',','] //因為該數組尚未被覆蓋。

parts2:[8,'experience','。'] //因為這是第二句中的跟蹤數據。

splitParts1:

我期望:[3,'clever',','] //由於第一個推送到數組的內容

但是我得到:[8,'experience','。'] //那是我不明白的

splitParts2:

我期望:[[3,'聰明',','],[8,'經驗','。']]

但我得到:[[8,'experience','。'],[8,'experience','。']]

為什么我丟失了第二個循環的第一個條目?

這是因為您要覆蓋循環中的parts 您將parts推入splitParts數組,但實際上將對parts數組的引用存儲在該處。 然后,當您更改parts數組時,它將更改splitParts數組中的內容。

如果將parts變量設置為內部循環的局部變量,則將得到所需的結果。

Javascript的工作方式定義了零件被覆蓋的行為。

價值傳遞

在Javascript中,原始類型(int,boolean,String,null,undefined,Symbol)在傳遞給函數時會按值傳遞,這意味着將創建一個新的內存空間並將其副本存儲在其中。

// demonstrating pass by value
function passByValuePrimitive(x) {
    // a new variable space is allocated in memory which holds the value 5 initially
    x = 6;// here you change the value of x to be 6 which points to the new variable created within this execution context.
    console.log(x)
}

var x = 5;
console.log(x); // value of x gets passed to the function.
passByValuePrimitive(x);
console.log(x); // here again in the global execution context, the value of x will be 5

通過參考

當我們考慮對象時,它們是通過引用傳遞的,這意味着將傳遞對象的內存地址,並且對該值的任何更改在程序的整個執行上下文中都是持久的。

// demonstrating pass by reference
function passByReference(x) {
    // the reference address of x is known to the function
    x.name = "Caesar";// here you change the value of x.name to be Caesar which points to the same memory address of the object x created in the global execution context.
    console.log(x)
}

var x = { name: "Julius"};
console.log(x); // Here the reference address of the object x in memory is passed to the function
passByReference(x);
console.log(x); // here in the global execution context, the value of x.name will be "Caesar"

希望能幫助到你。 @SBylemans提供了明確的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM