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

Collect.js keys() Method

The keys() method is used to return all of the collection’s keys.

Syntax:

collect(array).keys()

Parameters: The collect() method takes one argument that is converted into the collection and then keys() method is applied on it. 

Return Value: This method returns the collection keys.

Below example illustrate the keys() method in collect.js:

Example 1:




const collect = require('collect.js');
  
const collection = collect({
    name: 'Rahul',
    dob: '25-10-96',
    section: 'A',
    score: 98,
});
  
const keys_val = collection.keys();
  
console.log(keys_val.all());


Output:

[ 'name', 'dob', 'section', 'score' ]

Example 2:




const collect = require('collect.js');
  
let obj = [
    {
        name: 'Rahul',
        dob: '25-10-96',
        section: 'A',
        score: 98,
    },
    {
        name: 'Aditya',
        dob: '25-10-96',
        section: 'B',
        score: 96,
    },
    {
        name: 'Abhishek',
        dob: '16-08-94',
        section: 'A',
        score: 80
    },
    {
        name: 'Rahul',
        dob: '19-08-96',
        section: 'B',
        score: 77,
    },
];
  
const collection = collect(obj);
  
const keys_val = collection.keys();
  
console.log(keys_val.all());


Output:

[ 0, 1, 2, 3 ]

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!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments