Lodash _.mapArgsWith() 方法
Lodash _.mapArgsWith()方法接受一个映射函数并返回一个新的组合函数,该函数将接受一个目标函数并返回一个新函数,该函数在执行目标函数的主体之前将其参数与映射函数进行映射。
句法:
_.mapArgsWith( mapping_function );
参数:此方法接受如上所述并定义如下的单个参数:
- mapping_function:要被函数。
返回值:此方法返回一个函数。
注意:要执行以下示例,您必须使用此命令提示符安装lodash-contrib库并执行以下命令。
npm install lodash-contrib
下面的示例说明了 JavaScript 中的 Lodash _.mapArgsWith() 方法:
示例 1:我们创建了一个函数,该函数将给定值立方然后将该值添加到自身。
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function add (x)
{
return x + x + x ; }
function sub (x)
{
return x - 2;
}
var addnow = _.mapArgsWith(sub);
var subnow = addnow(add);
console.log(subnow(7))
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function squ (x)
{
return x * x ; }
function add (x)
{
return x + 10;
}
var addnow = _.mapArgsWith(add);
var sq = addnow(squ);
console.log(sq(5))
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function cs (x)
{
return "GeeksforGeeks : Computer Science Portal for Geeks" ; }
function geek (x)
{
return "GeeksforGeeks";
}
var gfg = _.mapArgsWith(geek);
var gfgFunc = gfg(cs);
console.log(gfgFunc())
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function cs (x)
{
return x; }
function geek (x)
{
return x[0]+" : "+x[1];
}
var gfg = _.mapArgsWith(geek);
var gfgFunc = gfg(cs);
console.log(gfgFunc(["GeeksforGeeks",
"Computer Science Portal for Geeks"]))
输出:
15
示例 2:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function squ (x)
{
return x * x ; }
function add (x)
{
return x + 10;
}
var addnow = _.mapArgsWith(add);
var sq = addnow(squ);
console.log(sq(5))
输出:
225
示例 3:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function cs (x)
{
return "GeeksforGeeks : Computer Science Portal for Geeks" ; }
function geek (x)
{
return "GeeksforGeeks";
}
var gfg = _.mapArgsWith(geek);
var gfgFunc = gfg(cs);
console.log(gfgFunc())
输出:
GeeksforGeeks : Computer Science Portal for Geeks
示例 4:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
function cs (x)
{
return x; }
function geek (x)
{
return x[0]+" : "+x[1];
}
var gfg = _.mapArgsWith(geek);
var gfgFunc = gfg(cs);
console.log(gfgFunc(["GeeksforGeeks",
"Computer Science Portal for Geeks"]))
输出:
GeeksforGeeks : Computer Science Portal for Geeks