Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptCollect.js pluck() Method

Collect.js pluck() Method

The pluck() method is used to return all the values from the given key.

Syntax:

collect(array).pluck(key)

Parameters: The collect() method takes one argument that is converted into the collection and then pluck() method is applied on it. The pluck() method holds the key as parameter.

Return Value: This method returns all values from the given key.

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

Example 1:

Javascript




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
    }
];
  
const collection = collect(obj);
  
const pluck_val = collection.pluck('name');
  
console.log(pluck_val.all());


Output:

[ 'Rahul', 'Aditya', 'Abhishek' ]

Example 2:

Javascript




const collect = require('collect.js');
  
let obj = [
    {
        name: 'Rahul',
        dob: '25-10-96'
    },
    {
        name: 'Aditya',
        dob: '25-10-96'
    },
    {
        name: 'Abhishek',
        dob: '16-08-94'
    }
];
  
const collection = collect(obj);
  
const pluck_val = collection.pluck('name', 'dob');
  
console.log(pluck_val.all());


Output:

{ '25-10-96': 'Aditya', '16-08-94': 'Abhishek' }

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