Monday, December 30, 2024
Google search engine
HomeLanguagesJavascriptCollect.js | combine() Method

Collect.js | combine() Method

Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection.
The combine() method combines two different arrays with keys-values.
 

Installation:  

  • Collect.js can be installed via NPM: 
npm install --save collect.js
  • You can also use CDN of collect.js 
<script src="https://cdnjs.com/libraries/collect.js"></script>

Syntax:  

collect(keys).combine(values)

Parameters: The collect() takes one argument that is keys converted into the collection and then combine() function takes one argument that are values
Return Value: Returns an object.
Below example illustrate the combine() methodin JavaScript: 
Example: Here collect = require(‘collect.js’) is used to import the collect.js library into the file. 

javascript




const collect = require('collect.js');
 
let keys = ['name', 'language']
 
let values = ['collect.js', 'js']
 
// convert keys into collection
const collection = collect(keys);
 
 
const object = collection.combine(values)
 
// returning the object
let newObject =  object.all();
 
console.log("Single array: ", newObject);


Output: 

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