Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash : return first key of object whose value(i.e Array) has a given element (i.e string) in it

Tags:

I have an object like:

var obj = {   "01": ["a","b"],   "03": ["c","d"],   "04": ["e","c"] }; 

and I know an array element ( say "c") of the object key's value then How to find first key value i.e "03" using lodash without using if else?

I tried like this using lodash and if else:

var rId = ""; _.forOwn(obj, function (array, id) {      if (_.indexOf(array, "c") >= 0) {            rId = id;            return false;      } });  console.log(rId); // "03" 

Expected Result: first key i.e "03" if element matches else "".

After seeing comments: Now I'm also curious to know about

Does I need to go with native javascript(hard to read program in the cases if we use more than 2 if blocks) or lodash way(easily readable program solution in one line)?

like image 903
Sandeep Sharma Avatar asked Apr 30 '16 19:04

Sandeep Sharma


People also ask

How do you get the first key of an object?

Use object. keys(objectName) method to get access to all the keys of object. Now, we can use indexing like Object. keys(objectName)[0] to get the key of first element of object.

How do I find unique elements in an array Lodash?

Lodash has the uniq method to return unique values from an array of objects. We call map to return an array of age values from array . And then we call uniq on the returned array to get the unique values from that returned array. So we get the same result for uniques as the other examples.

What is _ get?

Overview. The _. get() method in Lodash retrieves the object's value at a specific path. If the value is not present at the object's specific path, it will be resolved as undefined . This method will return the default value if specified in such a case.

How do I get the last element of an array using Lodash?

last() method is used to get the last element of the array i.e. (n-1)th element. Parameters: This function accepts single parameter i.e. the array. Return Value: It returns the last element of the array.


1 Answers

Since you just want a way to be able to find a key using a simple Lodash command, the following should work:

_.findKey(obj, function(item) { return item.indexOf("c") !== -1; }); 

or, using ES6 syntax,

_.findKey(obj, (item) => (item.indexOf("c") !== -1)); 

This returns "03" for your example.

The predicate function - the second argument to findKey() - has automatic access to the value of the key. If nothing is found matching the predicate function, undefined is returned.

Documentation for findKey() is here.


Examples taken from the documentation:

var users = {   'barney':  { 'age': 36, 'active': true },   'fred':    { 'age': 40, 'active': false },   'pebbles': { 'age': 1,  'active': true } };  _.findKey(users, function(o) { return o.age < 40; }); // → 'barney' (iteration order is not guaranteed)  // The `_.matches` iteratee shorthand. _.findKey(users, { 'age': 1, 'active': true }); // → 'pebbles'  // The `_.matchesProperty` iteratee shorthand. _.findKey(users, ['active', false]); // → 'fred'  // The `_.property` iteratee shorthand. _.findKey(users, 'active'); // → 'barney' 
like image 179
Akshat Mahajan Avatar answered Oct 04 '22 13:10

Akshat Mahajan