batteriesinfinity.com

Mastering JavaScript: Extracting Specific Object Properties

Written on

Chapter 1: Understanding Object Property Subsets

In JavaScript, it’s often necessary to extract specific properties from an object. This article explores various methods to achieve that.

JavaScript object properties visualization

Section 1.1: Using Object Destructuring

One of the simplest and most efficient ways to obtain a subset of properties from a JavaScript object is through destructuring. For example:

const object = {

a: 1,

b: 2,

c: 3

};

const {

a,

b

} = object;

const picked = {

a,

b

};

console.log(picked);

In this snippet, we have an object with three properties: a, b, and c. By destructuring, we assign the values of a and b to their respective variables and then create a new object picked, resulting in {a: 1, b: 2}.

Destructuring can also be utilized in function parameters, like so:

const pick = ({

a,

b

}) => ({

a,

b

});

const picked = pick(object);

console.log(picked);

Here, we define a function pick that returns an object containing the specified properties from the argument passed to it.

Section 1.2: Leveraging Lodash's Pick Method

Another method to extract properties is by using Lodash’s pick function. Here’s how it works:

const picked = _.pick(object, ['a', 'b']);

console.log(picked);

In this example, we call pick with the target object and an array of property names, resulting in the same output as before.

Section 1.3: Using Array.prototype.reduce

The reduce method can also be utilized to collect properties from an object into a new one. Consider the following example:

const picked = ['a', 'b'].reduce((resultObj, key) => ({

...resultObj,

[key]: object[key]

}), {});

console.log(picked);

In this case, we initiate reduce on an array containing the property names. The accumulator resultObj gathers the selected properties into a new object, yielding the expected result.

Conclusion

In summary, extracting a subset of properties from a JavaScript object can be efficiently accomplished using destructuring, Lodash’s pick, or the reduce method. Each of these techniques provides a straightforward approach to manage object properties effectively.

Check out this video for three different methods to access properties in JavaScript objects.

This video explains how to search for values in deeply nested JavaScript objects, a common interview question.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

How to Effectively Create a Calorie Deficit for Belly Fat Loss

Discover how to create a calorie deficit to accelerate belly fat loss and improve overall health with these practical strategies.

# 5 Compelling Reasons to Pursue iOS Development in 2024

Discover five key reasons to dive into iOS development in 2024, including high demand and lucrative salaries.

Exploring Animal Cognition: Can Animals Really Think?

This article delves into the complex question of animal cognition and the evolving scientific understanding of animal intelligence.

Navigating Social Media After a Breakup: Should You Unfollow?

Explore the complexities of unfollowing an ex on social media post-breakup and learn how to prioritize your emotional well-being.

Exploring the Origins: Can a Universe Emerge from Nothing?

Investigating the concept of a universe arising from nothingness and its implications in physics and cosmology.

Effective Strategies to Engage Readers on Medium for Longer

Discover effective techniques to keep Medium readers engaged beyond 30 seconds.

# Is Loneliness the New Smoking? Exploring the Epidemic

Investigating the rise of loneliness and its impact on health, exploring innovative solutions in companionship and friendship.

Unlocking the Power of Custom ChatGPT: A Guide to Effective Prompts

Discover how to craft effective prompts to create your customized ChatGPT and harness its full potential.