0

I create an object with two properties. Each property is a two dimensional array of strings. Below you can see how the arrays are filled and saved as properties.

let dataCodewords=["00100000", "01011011", "00001011", "01111000", "11010001", "01110010", "11011100", "01001101", "01000011", "01000000", "11101100", "00010001", "11101100", "00010001", "11101100", "00010001"];
let dataCodewordGroups={};
let position=0;

// blocksCodewords.group1Blocks[version-1] is 16 in this case
let group1=[];
for(let j=0; j<blocksCodewords.group1Blocks[version-1]; j++){
    let block=[];
  // blocksCodewords.blocks1codewords[version-1] is 1 in this case
    for(let k=0; k<blocksCodewords.blocks1codewords[version-1]; k++){
        block.push(dataCodewords[position]);
        position++;
    }
    group1.push(block);
}
dataCodewordGroups.group1=group1;

// blocksCodewords.group2Blocks[version-1] is 0 in this case
let group2=[];
for(let j=0; j<blocksCodewords.group2Blocks[version-1]; j++){
    let block=[];
  // blocksCodewords.blocks2codewords[version-1] is 0 in this case
    for(let k=0; k<blocksCodewords.blocks2codewords[version-1]; k++){
        block.push(dataCodewords[position]);
        position++;
    }
    group2.push(block);
}
dataCodewordGroups.group2=group2;

When logging the properties, the result is as I expected:

console.log(dataCodewordGroups.group1);
// >> ["00100000", "01011011", "00001011", "01111000", "11010001", "01110010", "11011100", "01001101", "01000011", "01000000", "11101100", "00010001", "11101100", "00010001", "11101100", "00010001"]

But logging the object changes the type of the stored data:

console.log(dataCodewordGroups);
// >> {[32, 91, 11, 120, 209, 114, 220, 77, 67, 64, 236, 17, 236, 17, 236, 17][]}

Why are the strings converted to numbers?

Shinji Ikari
  • 173
  • 1
  • 12
  • 1
    We need a [mcve] to diagnose. Right now, the code does not work, nor is it apparent how it would behave if it did. – VLAZ May 25 '21 at 07:56
  • 1
    Given the description, it seems *very likely* you're seeing [console.log() shows the changed value of a variable before the value actually changes](https://stackoverflow.com/q/11284663) / [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/q/4057440) – VLAZ May 25 '21 at 07:58

1 Answers1

0

The comment of VLAZ got it. I accidentially changed the data in a second function that is called later, so it seems the logging is just to slow.

Shinji Ikari
  • 173
  • 1
  • 12