Skip to content
Related Articles

Related Articles

Improve Article

Collect.js wrap() Method

  • Last Updated : 29 Jul, 2020
Geek Week

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




// 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());

Output:

Example 2:

Javascript




// 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());

Output:

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :