Saturday, August 30, 2025
HomeLanguagesJavascriptCollect.js | flip() function

Collect.js | flip() function

The flip() function swaps the key with its corresponding value. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.

Syntax: 

data.flip()

Parameters: This function does not accept any parameter.

Return Value:  Returns by flipping the collection.
 

Below examples illustrate the flip() function in collect.js
Example 1:  Here in this example, we take a collection and then using the flip() function we flip the key and the item in collection.

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const collection = collect({
  name: 'Jhon ',
  number: 000-555,
});
  
console.log(collection.flip());


Output:

Collection { items: { 'Jhon ': 'name', '-555': 'number' } }

Example 2:

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
const collection = collect({
    address : ['311-Street', 'US'],
    pin : 2546,
    number : 9898-585-598, 
});
  
const X  = collection.flip();
console.log(X.all());


Output:

{ '2546': 'pin', '8715': 'number', '311-Street,US': 'address' }

Reference: https://collect.js.org/api/count.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

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS