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 = 
        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)
        // return tiles
    function outlineRectSize(p_x, p_y, p_index, p_active, p_mainMap, p_tiles = []){
            this.i = p_tiles.length
                p_tiles[this.i] = widthFigure(p_x-1, 'x', p_tiles)
                p_tiles[this.i] = widthFigure(p_x+1, 'x', p_tiles)
                p_tiles[this.i] = widthFigure(p_y-1, 'y', p_tiles)
                p_tiles[this.i] = widthFigure(p_y+1, 'y', p_tiles)
        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 = []) => {
    return arr

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


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

  • 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.

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