How do i change the value of each property in an array with objects and return it?

Viewed 21

Let's say i first have a list of objects with properties of r, g, b values that would look something like this:

let color_pixels = [{r: 100, g: 20, b: 40}, {r: 80, g: 50, b: 30}];

let image = function(color_pixels) { 

}

How would i go about multiplying each property and updating the array with the multiplied properties in a function?

1 Answers

const colorPixels = [{r: 100, g: 20, b: 40}, {r: 80, g: 50, b: 30}];

const image = multiply(colorPixels, 1.5)


function multiply(pixels, factor) {
  return pixels.map(pixel => {r: pixel.r * factor, g: pixel.g * factor, b: pixel.b * factor})
}

Or modify the map callback return value as you need it.

Related