javascript 一行代码实现数组(array)中的值同时执行某个操作(javascrript the value of array performs an operation simultaneously by use a line if code)

 

一行代码实现 javascript 数组中的值同时执行某个操作,代码如下所示:

(1)实现 javascript 数组中的值同时除以某个数:

参考资料: https://www.includehelp.com/code-snippets/javascript-multiply-every-array-element-with-a-constant.aspx

1         start_point_row = [210, 420, 630, 840, 1050, 1260, 1470, 1680, 1890, 2100];
2         start_point_row_canvas = start_point_row.map(function(value){
3             return parseInt(value/4);
4         });
5         console.log("start_point_row_canvas = ", start_point_row_canvas);

结果:

 

(2)一行代码实现 javascript 数组中的对象中的 value 同时除以某个数:

参考资料:  https://stackoverflow.com/questions/14775942/how-do-you-divide-each-number-in-an-array-by-a-variable-in-javascript

1         var divisor = 4;
2         var array = [{"x":16},{"x":32},{"x":48}];
3         array1 = array.map(function(v){
4           return {x: v.x / divisor};
5         });
6         console.log("array1 = ", array1);

结果:

 

posted @ 2020-12-04 14:59  ttweixiao9999  阅读(112)  评论(0编辑  收藏  举报