简体   繁体   English

Javascript 推送功能在代码中似乎无法正常工作

[英]Javascript push function doesn't seem to work correctly in code

Problem statement : Clean the room function: Input [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20] , make a function that organizes these into individual array that is ordered.问题陈述:清理房间函数:输入[1,2,4,591,392,391,2,5,10,2,1,1,1,20,20] ,制作一个函数,将这些组织成有序的单个数组。 For example answer(ArrayFromAbove) should return:例如 answer(ArrayFromAbove) 应该返回:

 [[1,1,1,1],[2,2,2], 4,5,10,[20,20], 391, 392,591].

My Code:我的代码:

const arrNum = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20] ;

function org(arr) {
    let finalarr = [];
    let arrnew = [];
    let val = 0;
    arr.sort((function(a, b){return a-b}));
    //console.log(arr);
    for (i=0; i<arr.length; i++){
        if (arr[i] != 0) {
            val = arr[i];
            arrnew.length = 0;
            arrnew.push(arr[i]);
            arr[i] = 0;
            for (j=0; j<arr.length; j++){
                if (arr[j] == val && arr[j] != 0) {
                    arrnew.push(arr[j]);
                    arr[j] = 0;
                }
            }
            finalarr.push(arrnew);
            console.log(arrnew);
        }
    }
    return finalarr;
    console.log(finalarr)
}

org(arrNum)

But this doesn't seem to give desired answer : Not sure what I am doing wrong.但这似乎没有给出想要的答案:不确定我做错了什么。 I have found the other solutions but I need to know what is wrong in my code please.我找到了其他解决方案,但我需要知道我的代码有什么问题。

Objects and arrays are pushed as a pointer to the original object .对象和数组作为指向原始对象的指针推送。 Built-in primitive types like numbers or booleans are pushed as a copy.数字或布尔值等内置原始类型作为副本推送。

In your code your are pushing arrnew in finalarr在您的代码中,您正在arrnewfinalarr

finalarr.push(arrnew);

Which means reference of arrnew is pushed in finalarr .这意味着arrnew引用被推入finalarr

So after first iteration of for loop:所以在for循环的第一次迭代之后:

finalarr = [[1,1,1,1]]
arrnew = [1,1,1,1]

Here element of array finalarr is acutally pointer to array arrnew .这里数组finalarr元素finalarr是指向数组arrnew指针。

So, In the second iteration the statement所以,在第二次迭代中,语句

arrnew.length = 0

Will empty the array arrnew , as array finalarr have pointer to array it also gets modified:将清空数组arrnew ,因为数组finalarr有指向数组的指针,它也会被修改:

finalarr= [[]]

Then inner for loop will push three 2 in array arrnew = [2,2,2] , so finalarr become [[2,2,2]] .然后内部for循环将推动数组arrnew = [2,2,2]三个2 ,因此finalarr变为[[2,2,2]]

At the end of second iteration finalarr.push(arrnew);在第二次迭代结束时finalarr.push(arrnew); will push one new reference of arrnew in finalarr .将在finalarr推送一个新的arrnew finalarr So finalarr will become所以finalarr将成为

finalarr = [[2,2,2], [2,2,2]]

So at the end of all iterations, finalarr will have 9 pointers, all pointing to arrnew and the final value in arrnew is [591].因此,在所有迭代的结尾, finalarr将有9个指针,所有指向arrnew和最终值在arrnew为[591]。

finalarr = [[591],[591],[591],[591],[591],[591],[591],[591],[591]]

You can update you code, just replace arrnew.length = 0 with arrnew = []您可以更新代码,只需将arrnew.length = 0替换为arrnew = []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM