最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does lodash chaining work? - Stack Overflow

programmeradmin1浏览0评论

Lodash v4.17.2

Could someone please clarify how chaining works? I just haven't found any documentation or blogs that explain the return results from lodash methods. In the following code sample, why does the second assignment to filterAge not work? Or a similar more jquery like chaining? If anyone has a resource that explains this, I would love it.

let characters = [
  { 'name': 'barney1', 'age': 36, 'pets': ['hoppy'] },
  { 'name': 'barney2', 'age': 36, 'pets': ['hoppy'] },
  { 'name': 'barney3', 'age': 36, 'pets': ['hoppy'] },
  { 'name': 'barney4', 'age': 36, 'pets': ['hoppy'] },
  { 'name': 'fred',   'age': 40, 'pets': ['baby puss', 'dino'] }
];

let filterAge = _.take(_.slice(_.filter(characters, { 'age': 36 }), 2), 5);
filterAge = _.filter(characters, { 'age': 36 })
          .slice(2)
          .take(5);

console.log(JSON.stringify(filterAge));
<script src=".17.2/lodash.min.js"></script>

Lodash v4.17.2

Could someone please clarify how chaining works? I just haven't found any documentation or blogs that explain the return results from lodash methods. In the following code sample, why does the second assignment to filterAge not work? Or a similar more jquery like chaining? If anyone has a resource that explains this, I would love it.

let characters = [
  { 'name': 'barney1', 'age': 36, 'pets': ['hoppy'] },
  { 'name': 'barney2', 'age': 36, 'pets': ['hoppy'] },
  { 'name': 'barney3', 'age': 36, 'pets': ['hoppy'] },
  { 'name': 'barney4', 'age': 36, 'pets': ['hoppy'] },
  { 'name': 'fred',   'age': 40, 'pets': ['baby puss', 'dino'] }
];

let filterAge = _.take(_.slice(_.filter(characters, { 'age': 36 }), 2), 5);
filterAge = _.filter(characters, { 'age': 36 })
          .slice(2)
          .take(5);

console.log(JSON.stringify(filterAge));
<script src="https://cdn.jsdelivr/lodash/4.17.2/lodash.min.js"></script>

fiddle

Share Improve this question edited Dec 19, 2016 at 6:21 JohnWrensby asked Dec 18, 2016 at 21:40 JohnWrensbyJohnWrensby 2,8721 gold badge19 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can find documentation on chaining or sequences in the description for _(value) and the library's other "Seq" functions/methods.

To chain with lodash, you'll want to use _(value) or _.chain(value) to create a sequence and .value() at the end to retrieve the result.

In between those, each of lodash' other functions will be available as a method of the sequence, with 2 differences:

  • The methods return a modified sequence object that holds the current value rather than the value itself, with the exception of the .value() method.

  • The 1st argument has been removed. Each method gets the value to work on from the sequence object before it rather than from the arguments. And, the other arguments you pass to the functions are shifted one position to the left for the methods.

For your example:

filterAge = _.chain(characters)
    .filter({ 'age': 36 })
    .slice(2)
    .take(5)
    .value();

In order to chain with lodash, you need to wrap your object first with _() call - this would allow to call any lodash method on your wrapped instance. Then, in order to get the value, you'd need to call value method on your wrapped object. So your example would look like this:

filterAge = _(characters).filter({ 'age': 36 })
                         .slice(2)
                         .take(5).value();

Without wrapping your object, you cannot chain function calls, because you would always return just the array where lodash methods like take are not implemented.

发布评论

评论列表(0)

  1. 暂无评论