javascript 一行代码实现数组(array)中的值同时执行某个操作(javascrript the value of array performs an operation simultaneously by use a line if code)
一行代码实现 javascript 数组中的值同时执行某个操作,代码如下所示:
(1)实现 javascript 数组中的值同时除以某个数:
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 同时除以某个数:
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);
结果: