简体   繁体   English

如何使用对象更改数组中每个属性的值并返回它?

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

Let's say i first have a list of objects with properties of r, g, b values that would look something like this:假设我首先有一个对象列表,其属性为 r、g、b 值,如下所示:

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?我将如何 go 将每个属性相乘并使用 function 中的相乘属性更新数组?

 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.或者根据需要修改 map 回调返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在对象数组中,如何在 React 中按属性返回所有对象? - In an array of Objects, how do I return all Objects by property in React? 如何定位数组中的对象子集并将属性更改为每个对象的递增数字? - How can I target subset of objects in array and change a property to an incrementing number for each of those objects? 如何使用对象更改数组中的一个属性值 - How to change one property value in array with objects 在javascript-map()方法中更改对象数组中每个属性的值? - Change value of each property in array of objects in javascript - map() method? 如何更改数组中 object 的 boolean 属性的值? - How do I change the value of a boolean property of an object in an array? 如何遍历对象数组以返回依赖于 object 中相应属性的属性? - How do I loop over an array of objects to return a property dependent on a corresponding property within that object? 如何根据属性值过滤数组并返回对象数组 - How to filter an array based on property value and return array of objects 如何过滤嵌套在数组对象属性中的对象数组? - How do i filter array of objects nested in property of array objects? 如何更改对象数组中数组中的每个值? - How to change each value from an array inside an array of objects? 如何返回属性与数组匹配的对象数组? - How can I return an array of objects whose property matches an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM