Sunday, October 6, 2024
Google search engine
HomeLanguagesJavascriptCollect.js wrap() Method

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




// 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments