Collect.js wrap() Method

The wrap() method in collect.js is used to wrap a particular set of values in a collection.

Installation:

  • In NodeJs:
    npm install collect.js
  • CDN for collect.js
    <script src="https://cdnjs.com/libraries/collect.js"></script>

Syntax:

wrap( object );

Parameters: This method accepts an array or object.

Return Value: It returns the object.



Example 1:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Importing the collect.js module.
const collect = require('collect.js');
let obj1 = { "a": 1, "b": 12, "c": 3 };
  
// This will make a empty collection
let collection = collect();
  
// It will not wrap
collection.wrap(obj1);
  
console.log(collection.all());
  
// Using wrap function to wrap values
// to a collection
collection = collect().wrap(obj1);
console.log(collection.all());

chevron_right


Output:

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Importing the collect.js module.
const collect = require('collect.js');
let obj1 = { "a": 1, "b": 12, "c": 3 };
let obj2 = { "aa": 1, "bb": 12, "cc": 3 };
let obj3 = { "aaa": 1, "bbb": 12, "ccc": 3 };
  
// Using wrap() method to wrap values
// to a collection
collection = collect().wrap(obj1)
    .wrap(obj2)
    .wrap(obj3);
  
// Note: only the obj3 is wrapped and
// obj1 and obj2 are replaced
  
console.log(collection.all());
collection = collect().wrap(obj1)
    .wrap(obj2)
  
// Note: only the obj2 is wrapped 
// and obj1 is replaced.
console.log(collection.all());

chevron_right


Output:

Reference: https://collect.js.org/api/wrap.html

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.