1

In Typescript:

I have a 1D array of objects: my1dArray.

I am copying 5 x each row of my1dArray into a 2D array my2dArray (so I end up with a my2dArray[3][5]).

I then reset the id element for each row of the my2dArray.

For my2dArray[0], I end up with this (id=4):

{id: 4, surname: "Smith", givenNames: "Linda"}
{id: 4, surname: "Smith", givenNames: "Linda"}
{id: 4, surname: "Smith", givenNames: "Linda"}
{id: 4, surname: "Smith", givenNames: "Linda"}
{id: 4, surname: "Smith", givenNames: "Linda"}

What I thought I would get is this:

{id: 0, surname: "Smith", givenNames: "Linda"}
{id: 1, surname: "Smith", givenNames: "Linda"}
{id: 2, surname: "Smith", givenNames: "Linda"}
{id: 3, surname: "Smith", givenNames: "Linda"}
{id: 4, surname: "Smith", givenNames: "Linda"}

The rest of the my2dArray[4][5] array has the same problem.

What am I doing wrong?

//Create 1d Array
let my1dArray: any[];
my1dArray = [

      { id: 0, surname: 'Smith', givenNames: 'Linda'},
      { id: 0, surname: 'Bloggs', givenNames: 'Bill'},
      { id: 0, surname: 'Jones', givenNames: 'Jim'},

];


//Initalise 2d Array
let my2dArray = init2DArray(3, 5, function () {

      return 0

});


let j = 0;
let k = 0;
for (let i = 0; i < my1dArray.length; i++) {

      for (let count = 0; count < 5; count++) {

          my2dArray[j][count] = my1dArray[i];
          //reset the id
          my2dArray[j][count].id = k;
          k++;
      }



    j++;

}

console.log('myNewData', my2dArray[0]);




/////Intialisation function///////////////

function init2DArray(xlen, ylen, factoryFn) {

      let ret = [];

      for (let x = 0; x < xlen; x++) {
          ret[x] = [];
          for (let y = 0; y < ylen; y++) {
              ret[x][y] = factoryFn(x, y)
          }
      }

  return ret
}
shizhen
  • 12,251
  • 9
  • 52
  • 88
robs
  • 101
  • 1
  • 2
  • 10

1 Answers1

0

Actually you don't copy each row of my1dArray. This assignment:

my2dArray[j][count] = my1dArray[i];

only fills my2dArray with references to the same object in my1dArray. Therefore when you change the id you do this for the same object every time in your inner loop.

To correctly copy an object in JS you may want to read this. For shallow copy (sufficient for your example code) you can use Object.assign().

Below a working code snippet:

//Create 1d Array
let my1dArray: any[];
my1dArray = [{
    id: 0,
    surname: 'Smith',
    givenNames: 'Linda'
  },
  {
    id: 0,
    surname: 'Bloggs',
    givenNames: 'Bill'
  },
  {
    id: 0,
    surname: 'Jones',
    givenNames: 'Jim'
  },
];


//Initalise 2d Array
let my2dArray = init2DArray(3, 5, function() {
  return 0
});

let j = 0;
let k = 0;
for (let i = 0; i < my1dArray.length; i++) {
  for (let count = 0; count < 5; count++) {
    my2dArray[j][count] = Object.assign({}, my1dArray[i]);
    //reset the id
    my2dArray[j][count].id = k;
    k++;
  }
  j++;
}


function init2DArray(xlen, ylen, factoryFn) {
  let ret = [];
  for (let x = 0; x < xlen; x++) {
    ret[x] = [];
    for (let y = 0; y < ylen; y++) {
      ret[x][y] = factoryFn(x, y)
    }
  }
  return ret
}

console.log(my2dArray)
Beniamin H
  • 2,048
  • 1
  • 11
  • 17