我如何重写功能代码 javascript
How do i rewriting a function code javascript
returns a new array
包含 each pair of numbers
来自具有 相同索引 的参数的乘积的函数。
var a = [3, 5, 7];
var b = [9, 10, 11];
a.map(function(x, index) { //here x = a[index] //how do i write this as function name(){}
console.log(b[index] * x);
});
这样
var a = [3, 5, 7];
var b = [9, 10, 11];
function myFunction(x,index)
{
return (b[index] * x);
}
var c = a.map(myFunction);
console.log( c )
var a = [3, 5, 7];
var b = [9, 10, 11];
let c = [];
function name(a, b) {
a.map(function(x, index) {
let result = b[index] * x;
c.push(result);
}
}
name(a, b);
console.log(c) // this will show a new array with the results
您需要 return 来自 map
回调的新值。
var a = [3, 5, 7];
var b = [9, 10, 11];
const res = a.map((x,index)=>b[index] * x);
console.log(res);
.map()
的规格说明:
The map() method creates a new array
populated with the results of
calling a provided function on every element in the calling array.
var a = [3, 5, 7];
var b = [9, 10, 11];
function name(value_of_array_a, index_of_array_b) {
return b[index_of_array_b] * value_of_array_a;
}
var result = a.map(name);
console.log(result);
returns a new array
包含 each pair of numbers
来自具有 相同索引 的参数的乘积的函数。
var a = [3, 5, 7];
var b = [9, 10, 11];
a.map(function(x, index) { //here x = a[index] //how do i write this as function name(){}
console.log(b[index] * x);
});
这样
var a = [3, 5, 7];
var b = [9, 10, 11];
function myFunction(x,index)
{
return (b[index] * x);
}
var c = a.map(myFunction);
console.log( c )
var a = [3, 5, 7];
var b = [9, 10, 11];
let c = [];
function name(a, b) {
a.map(function(x, index) {
let result = b[index] * x;
c.push(result);
}
}
name(a, b);
console.log(c) // this will show a new array with the results
您需要 return 来自 map
回调的新值。
var a = [3, 5, 7];
var b = [9, 10, 11];
const res = a.map((x,index)=>b[index] * x);
console.log(res);
.map()
的规格说明:
The map() method
creates a new array
populated with the results of calling a provided function on every element in the calling array.
var a = [3, 5, 7];
var b = [9, 10, 11];
function name(value_of_array_a, index_of_array_b) {
return b[index_of_array_b] * value_of_array_a;
}
var result = a.map(name);
console.log(result);