I have an object like this:
const obj = {
'MONEY-OUT': {
USD: ['a', 'b', 'c'],
EUR: ['d', 'e', 'f'],
},
'MONEY-IN': {
USD: ['g', 'h', 'i'],
EUR: ['j', 'k', 'l'],
},
};
and I want to create a function that returns either the key MONEY-OUT
/ MONEY-IN
if that key contains a character in its array using Lodash.
I have 2 variables in this function: currency, search_string
For example, my variable values for this function are ‘EUR’ and ‘k’ so I need my function to return the key MONEY-IN
2
Answers
There’s zero reason to use Lodash or any other utility library for this.
You can use Array.prototype.find() to find the first object entry (key / value) pair matching your criteria, then return the key for that entry
Since the answer you are searching for a
lodash
based solution and above@Phil provided answer is based upon
Array.prototype.find()
, Here’s how you can achieve what you are looking for using the librarylodash
:Refer the below code for reference: