Set the first 10 value of the typedArray to 1000

89 Views Asked by At

Here I'm using a TypedArray Uint32Array. Where I have some set of data in the data. I want to replace the first 10 data of the array with some value. I'm trying to set it to the first 10 value but it is not binding respective to the first 10 data of the array. Kindly, help me out. Thanks in advance.

var arrD = new Uint32Array(32)

arrD.slice(0, 12).forEach(function (item) {
  arrD[item] = 5
  console.log(arrD)
})
1

There are 1 best solutions below

0
On

You could use the .fill array function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill

var arrD = new Uint32Array(32);
arrD.fill(5, 0, 10) // fills array with value from index 0 - 9