Find Objects by Nested Properties with Lodash
source link: https://masteringjs.io/tutorials/lodash/find-nested-object
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Find Objects by Nested Properties with Lodash
If you need to search for a nested object, you can use Lodash's .find()
function.
It takes three arguments:
collection
: which can be either an array or object.predicate
: the callback function that Lodash calls on every element in the array.fromIndex
: the index to search from. Defaults to 0.
Lodash will return the first element for which predicate
returns a truthy value, or undefined
if there's no such element.
You can write a predicate
that checks whether an element has a certain nested property.
The following code finds objects by the name.first
property.
const _ = require('lodash');
const obj = [
{
name: {
first: 'Test',
last: 'Testerson'
},
age: 2,
location: 'Florida'
},
{
name: {
first: 'Obi-wan',
last: 'Kenobi'
},
age: 45,
location: 'Tatooine'
},
{
name: {
first: 'Masteringjs',
last: '.io'
},
age: 5,
location: 'Florida'
}
];
let result = _.find(obj, el => el.name.first === 'Test');
result; // { name: { first: 'Test', last: 'Testerson', age: 2 }, location: 'Florida' }
result = _.find(obj, el => el.name.first === 'something else');
result; // undefined
To avoid cases where el.name
is null
or undefined
, you can use optional chaining ?.
, or Lodash's get()
function.
let result = _.find(obj, el => el?.name?.first === 'Test');
// Equivalent:
result = _.find(obj, el => _.get(el, 'name.first') === 'Test');
More Lodash Tutorials
Recommend
-
52
My favorite Lodash methods replaced by JavaScript native methods
-
57
You searched how to tree shake with Parcel and didn’t find it. It’s quick and easy. Every ones in a while we start some project and we start importing libraries. Project start to grow more and more and then we take a loo...
-
84
-
11
PowerShell - Control What Order Properties Are Displayed On A Custom Objects And Hash Tables There are a handful of different ways to create custom objects in PowerShell, including building one from a hash table. You mig...
-
10
Sort array of objects by multiple properties with Swift Tuple Table of ContentsIn my last article, How to sort by multiple prope...
-
5
This post is part of my Byte Series, where I document tips, tricks, and tools that I've found useful.ProblemImagine you have an array of JavaScript objects and you need...
-
17
1. Introduction ChoETL is an open source ETL (extract, transform and load) fra...
-
4
How to use destructuring in JavaScript to access deeply nested data How to destructure objects Let’s first go over how to destructure objects in JavaScript. We can start with this simple example of an object re...
-
4
ANKIT CHAUHAN January 23, 2023 Less than a 1 minute...
-
6
Nick ScialliMarch 20, 2023The Array.sort method can be challenging to grok, especially when you have to sort multiple properties. In this post, we’ll go through the mechanics and have you sorting like a pro! Ho...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK