Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
lodash _.findKey equivalent in Javascript or jQuery

I have a series of pieces in a game and at the beginning I store their store DIV complete with styling in an object with the respective key being an encrypted number. At the end of the game I check the piece that has been clicked on to see if its current z-index matches that of the original styling stored in the object. Currently I am using lodash which works perfectly but I would like to remove lodash so as not to have to load this extra libary. My problem is, I simply can't translate the three lines of code into pure Javascript (or jQuery for that matter) - I'm quite a beginner and have now spent hours trying to resolve this. Any advice would be appreciated. Here my "lodash" code:

var myVar = _.findKey(myObject, function (storedDIV) {
    return storedDIV.css('z-index') == clickedDIV.css('z-index');
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create a similar function to _.findKey() using Object.keys and Array.find(). The function accepts a predicate, or you can use the default identity predicate (o => o), but it doesn't support the other lodash _.findKey() shorthands.

const findKey = (obj, predicate = o => o) => Object.keys(obj)
    .find(key => predicate(obj[key], key, obj))

// example 1 - passing a predicate
const users = {'barney':  { 'age': 36, 'active': true },'fred':    { 'age': 40, 'active': false },'pebbles': { 'age': 1,  'active': true }}
console.log(findKey(users, o => o.age < 40)) // barney

// example 2 - using default identity predicate
const flags = { a: false, b: true, c: false }
console.log(findKey(flags)) // b

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!