john mangual john mangual - 1 month ago 24
Javascript Question

forEach for Uint8ClampedArray

The getImageData function returns a special array object called

which inherits many of the familiar array methods.
ForEach
points you to the generic method.

Let's try one:

var g = new Uint8ClampedArray([1,2,3,4,5]);
undefined

g
[1, 2, 3, 4, 5]

g.forEach(function(x){ return x + 1; } )
undefined

g
[1, 2, 3, 4, 5]


Nothing happened. The answer should be
[2,3,4,5,6]
what is wrong here?




This was done in Chrome's JavaScript terminal -- in the developer tools.

Also I found this helpful:


The
Uint8ClampedArray
typed array represents an array of 8-bit unsigned integers clamped to 0-255; if you specified a value that is out of the range of [0,255], 0 or 255 will be set instead. The contents are initialized to 0. Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).

Answer

forEach doesn't return any value, it's basically just a for loop over the contents. If you want to do a 1-1 transform and return a new array, then use the map method.