Sunday, June 20, 2021

Two diminsion array

Initial two dimension array with 0L

 > arr = Array(2).fill(Array(10).fill(0));
[
  [
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0
  ],
  [
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0
  ] 

However, assign a data that populate all rows in that position.

> arr[1][2]=3;
3
> arr
[
  [
    0, 0, 3, 0, 0,
    0, 0, 0, 0, 0
  ],
  [
    0, 0, 3, 0, 0,
    0, 0, 0, 0, 0
  ]
]


Initial two dimension array with sequence number:

 > arr = Array(2).fill(Array(10).fill(0)).map((a,i)=>a.map((b,j)=>i*10+j));
[
  [
    0, 1, 2, 3, 4,
    5, 6, 7, 8, 9
  ],
  [
    10, 11, 12, 13, 14,
    15, 16, 17, 18, 19
  ]
]

Reassigning a data is fine in here.

 > arr[1][2] = 77;
77
> arr
[
  [
    0, 1, 2, 3, 4,
    5, 6, 7, 8, 9
  ],
  [
    10, 11, 77, 13, 14,
    15, 16, 17, 18, 19
  ]
]



No comments: