Collect.js wrap() Method

Last Updated : 29 Jul, 2020

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