📜  Lodash _.prototype.chain() 方法

📅  最后修改于: 2022-05-13 01:56:25.953000             🧑  作者: Mango

Lodash _.prototype.chain() 方法

Lodash是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。

lodash 中 Sequence 的_.prototype.chain () 方法用于创建lodash包装器的实例,同时启用显式方法链序列。

句法:

_.prototype.chain()

参数:此方法不接受任何参数。

返回值:此方法返回新的lodash包装器实例。

示例 1:

Javascript
// Requiring lodash library
const _ = require('lodash');
  
// Initializing authors variable
var authors = [
  { 'author': 'Nidhi', 'articles': 750 },
  { 'author': 'Nisha', 'articles': 500 }
];
   
// Calling chain() method and creating
// an explicit chaining sequence
let result = _(authors).chain()
                       .tail()
                       .value();
  
// Displays output                       
console.log(result);


Javascript
// Requiring lodash library
const _ = require('lodash');
  
// Initializing authors variable
var authors = [
  { 'author': 'Nidhi', 'articles': 750 },
  { 'author': 'Nisha', 'articles': 500 }
];
   
// Calling chain() method and creating
// an explicit chaining sequence
let result = _(authors).chain()
                       .head()
                       .pick('articles')
                       .value();
  
// Displays output                       
console.log(result);


Javascript
// Requiring lodash library
const _ = require('lodash');
   
// Calling chain() method and creating
// an explicit chaining sequence
let obj = _("GeeksforGeeks").chain().value();
  
// Displays output                       
console.log(obj[0]);
console.log(obj[4]);
console.log(obj[7]);
console.log(obj[11]);


输出:

[ { author: 'Nisha', articles: 500 } ]

示例 2:

Javascript

// Requiring lodash library
const _ = require('lodash');
  
// Initializing authors variable
var authors = [
  { 'author': 'Nidhi', 'articles': 750 },
  { 'author': 'Nisha', 'articles': 500 }
];
   
// Calling chain() method and creating
// an explicit chaining sequence
let result = _(authors).chain()
                       .head()
                       .pick('articles')
                       .value();
  
// Displays output                       
console.log(result);

输出:

{ articles: 750 }

示例 3:

Javascript

// Requiring lodash library
const _ = require('lodash');
   
// Calling chain() method and creating
// an explicit chaining sequence
let obj = _("GeeksforGeeks").chain().value();
  
// Displays output                       
console.log(obj[0]);
console.log(obj[4]);
console.log(obj[7]);
console.log(obj[11]);

输出:

G
s
r
k

参考: https://lodash.com/docs/4.17.15#prototype-chain