简体   繁体   中英

How to create Lodash collection

There is a pretty convenient way in underscore for creating underscore collection (wrapper on array):

var x = _([{"name": "John"}]);
x.findWhere({"name": "John"}); // use any underscore methods now
x; // x is underscore collection, containing one item

But Lodash 's _() creates chain. It's rather inconvenient:

var x = _([{"name": "John"}]);
x.findWhere({"name": "John"}); // use any underscore methods now
x; // chain, currently empty
x.commit(); // x is Lodash wrapper, containing one item

The question is:

How to create simple collection in Lodash but not the chain?

You can use commit() in the declaration and you'll get the wrapper:

var x = _([{"name": "John"}]).commit();
x.findWhere({"name": "John"}); // << this works
x; // wrapper

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM