How to Filter Keys of an Object with Lodash ?
Last Updated :
29 Apr, 2024
Filtering Keys of an Object is used for selectively including or excluding keys based on criteria, facilitating data manipulation and customization.
Below are the approaches to filter keys of an object with Lodash:
Installing the Lodash library using the below command:
npm i lodash
Using pick function
In this approach, we are using the pick function from Lodash with an object and an array of keys to include, creating a new object res that contains only the specified keys 'a' and 'c' from the original object obj.
Syntax:
_.pick(object, [props])
Example: The below example uses the pick function to filter the keys of an object with lodash.
JavaScript
const _ = require('lodash');
let obj = { a: 1, b: 2, c: 3 };
let res = _.pick(obj, ['a', 'c']);
console.log(res);
Output:
{ a: 1, c: 3 }
Using omit function
In this approach, we are using the omit function from Lodash with an object and an array of keys to exclude, creating a new object res that contains all keys except 'b' from the original object obj.
Syntax:
_.omit(object, [props])
Example: The below example uses an omit function to filter the keys of an object with lodash.
JavaScript
const _ = require('lodash');
let obj = { a: 1, b: 2, c: 3 };
let res = _.omit(obj, ['b']);
console.log(res);
Output:
{ a: 1, c: 3 }
Using pickBy function
In this approach, we are using the pickBy function from Lodash with an object and a predicate function, creating a new object res that includes keys based on the condition specified in the predicate, which excludes the key 'b' from the original object obj.
Syntax:
_.pickBy(object, [predicate=_.identity])
Example: The below example uses the pickBy function to filter the keys of an object with lodash.
JavaScript
const _ = require('lodash');
let obj = { a: 1, b: 2, c: 3 };
let res = _.pickBy(obj, (value, key)
=> key !== 'b');
console.log(res);
Output:
{ a: 1, c: 3 }
Similar Reads
How to Filter Key of an Object using Lodash? Filtering keys of an object involves selecting specific keys and creating a new object that contains only those keys. Using Lodash, this process allows you to include or exclude properties based on specific criteria, simplifying object manipulation. Below are the approaches to filter keys of an obje
2 min read
How to Filter Object by Keys or Values in Lodash? Filtering objects is a common requirement when we want to extract certain data from larger datasets. Lodash provides convenient methods to do this, which helps us in avoid writing complex loops and conditional statements. Below are different approaches to filter objects by keys or values in lodash:T
3 min read
How to Filter Nested Object in Lodash? Lodash is a popular JavaScript utility library that simplifies common tasks like manipulating arrays, objects, strings, and more. In this tutorial, we will explore how to filter nested objects using Lodash's _.filter() and _.get() methods. This technique is useful when we need to filter data, such a
2 min read
How to Filter Array of Objects with Lodash Based on Property Value? Sometimes, We need to filter an array of objects based on specific property values. Lodash, a powerful utility library, provides efficient methods for such operations. we will explore how to use Lodash to filter an array of objects by property value, ensuring that you can easily extract and work wit
2 min read
How to Convert Object to Array in Lodash ? Converting an Object to an Array consists of changing the data structure from key-value pairs to an array format. Below are the different approaches to converting objects to arrays in Lodash: Table of Content Using toArray function Using values functionRun the below command before running the below
2 min read