I have 2 multidimensional arrays. I want to insert the small array values into the larger array at a specific location.
var largeArray = [...Array(4)].map(e => Array(4).fill(0));
var smallArray = [...Array(2)].map(e => Array(2).fill(1));
This is very similar to canvas context putImageData() function, where you can insert a block of values anywhere on the canvas. https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/putImageData
However, I am not using a canvas and I want to "PutSmallArray" into the large array a specific X,Y location.
How can I do this?
A simplistic way to achieve what you want is just to iterate over the
smallArray
values and indexes, using them to insert those values at the appropriate offset intolargeArray
:Note that this code doesn't check whether
smallArray
fits insidelargeArray
at thex,y
coordinates specified. If that might not be the case, then dependent on the result you desire (an unmodified array or only the elements that fit modified), you will need to check whether the indexes fall outside the bounds oflargeArray
e.g.