Categories
Mastering Development

Losing data from array

I’m slicing 3 elements from an array and storing them to another array

array = [1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1];
rows = 3;

Here is my method

getVertWallStruct = (array, rows) => {
    let i = 1,
        storageArr = [],
        data = [];

    for (let k = 0; k < rows*2; k++) { // everything's ok here
      storageArr.push(array.slice(k*rows, (k+1)*rows));
    }

    data = storageArr;

    console.log("storageArr - ", storageArr, " , array - ", array, " , data - ", data);

    return data;
  }

In this case storageArr will consist of empty arrays (as far as data). But when I delete line with data = storageArr; I get:

storageArr =  [ //storageArr should look like this in the end
  [1, 1, 1],
  [0, 1, 1],
  [1, 1, 1],
  [1, 1, 1],
  [0, 1, 1],
  [1, 1, 1]
]

Why do I lose values?

Leave a Reply

Your email address will not be published. Required fields are marked *