The array constant is padded, although there is no explicit conversion. How to solve?

  • 0
    const tiles = outlineRectSize(x, y, index, active, mainMap)
    This line in the first call is processed normally. But when you call only the function outlineRectSize ('arguments') later in the code, which is called a second time, but equates to the constant tiles - when viewed in the console, it acquires new elements within itself while maintaining the previous ones.
    Here's the whole code:
    x = 1
        y = 3
        index = 4
        mainMap = 
            [
                [5,5,4,4,4,5],
                [4,5,4,4,4,4],
                [4,3,3,3,3,4],
                [4,2,2,2,2,4],
                [4,4,4,4,4,4]
            ]
        active = true
    
    
    const tiles = outlineRectSize(x, y, index, active, mainMap)
    
    
    let roundTile = function (){
    
        
        for(let i = 0; tiles.from_pass.length > i; i++){
        
            
            if(tiles.from_pass[i] != undefined){
    
    
                this.currX = tiles.from_pass[i].match(/x:([0-9]{1,500})/)[1]
                this.currY = tiles.from_pass[i].match(/y:([0-9]{1,500})/)[1]
    
                if(tiles.passed.indexOf(`x:${this.currX}, y:${this.currY}`) == -1){
    
                    outlineRectSize(this.currX, this.currY, index, active, mainMap, tiles.from_pass)
                }
            }
        }
    
    
       console.log(tiles)
        // return tiles
    }
    
    roundTile()
    
    
    function outlineRectSize(p_x, p_y, p_index, p_active, p_mainMap, p_tiles = []){
    
    
        if(p_active){
    
            this.i = p_tiles.length
    
            if(p_mainMap[p_x-1]){
    
                p_tiles[this.i] = widthFigure(p_x-1, 'x', p_tiles)
                this.i++
            }
            if(p_mainMap[p_x+1]){
    
                p_tiles[this.i] = widthFigure(p_x+1, 'x', p_tiles)
                this.i++
            }
            if(p_mainMap[p_y-1]){
    
                p_tiles[this.i] = widthFigure(p_y-1, 'y', p_tiles)
                this.i++
            }
            if(p_mainMap[p_y+1]){
    
                p_tiles[this.i] = widthFigure(p_y+1, 'y', p_tiles)
                this.i++
            }
       
        return {
    
            from_pass: p_tiles, 
            passed:    [`x:${p_x}, y:${p_y}`]
    
            }
    
        }
           
    
    
    
        function widthFigure(beam, type, p_tiles){
    
            if(type == 'y'){
    
                this.tile = p_mainMap[p_x][beam]
                this.x = p_x
                this.y = beam
            }
    
            if(type == 'x'){
    
                this.tile = p_mainMap[beam][p_y]
                this.x = beam
                this.y = p_y
            }
    
            this.coords = `x:${this.x}, y:${this.y}`
    
            if(this.tile == p_index && p_tiles.indexOf(this.coords) == -1) return this.coords
    
    
        }
    
    
    
    }


    as you can see in the code, there is no equalization.
    JavaScript Micah Hale, Mar 15, 2020

  • 2 Answers
  • 0
    outlineRectSize uses p_tiles, which you pass tiles.from_pass
    on the second call
    And then inside the function you directly change (supplement) the values ​​in p_tiles

    To keep the values ​​from changing, you need to make a copy of the array inside the function. For example via JSON.parse ()



    const copyOfArray = JSON.parse(JSON.stringify(sourceArray))


    Second point: inside the roundTile function, you actually have the re-call of outlineRectSize. What for? If you don't want to change the array. And if you still want to, then why doesn't the call return to the variable?



    Here's some sample code to understand what's going on.

    const f = (arr = []) => {
    arr.push(1)
    arr.push(2)
    return arr
    }

    const basicArr = f([]) // [1,2]

    f(basicArr)

    console.log(basicArr) // [1, 2, 1, 2]
    Anonymous

  • 0
    I didn’t go deep into the code, but I think the reason is something like this - when you first call outlineRectSize, you have empty p_tiles parameter and it is saved in tiles.from_pass. On subsequent calls, you pass it as the last parameter and it is reused.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!