DEV Community

Discussion on: Lodash and Underscore, is there still a case?

Collapse
 
amyblankenship profile image
AmyBlankenship • Edited

I don't see anyone talking about what to me is the biggest advantage of lodash--the various syntaxes you can use for iteratees. So, _.map(myObj, 'someProp.innerArray[0]'), or _.find(someArray, {color: 'green'}) just completely blows the hell out of anything you could write yourself in the 10 seconds it took me to write that. And then there's the fact that the Collection methods let you treat arrays and objects as basically the same.

Also, studying the lodash docs really gives you a feel for what's possible in functional programming and some ways you might want to use it to solve your own problems. I think really understanding the lodash docs made me a much better programmer. I'd highly recommend this for people who don't already automatically reach for FP on a regular basis.

To me the downside is the cyclomatic complexity is unknown for all the functions. So you have no idea what the performance impact is of deciding to use a lodash function, whereas you do know for your own.

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

The first part of what you mentioned, I'm not convinced that this is not already covered by popular es6 array methods unless I am missing something?

Collapse
 
amyblankenship profile image
AmyBlankenship • Edited

Can you provide an example of syntax where you'd provide a string with the path to a deeply nested object property to use in ES6 map or where you'd be able to provide an object that describes a property and value to filter on? If such exists, I have never seen it described anywhere. AFAIK those methods always take a function.

Thread Thread
 
adam_cyclones profile image
Adam Crockett πŸŒ€

I don't think one method does exist off the top of my head. Can you give me a data structure snippet to visualize.

I guess you might want to use entries and perhaps flat, not working on nested data but flat copies, that's got to be more efficient.. maybe. IDK but I agree that convince sounds like the winner here. Anyway I always check this sort of opinion, it's interesting. 😁

Thread Thread
 
amyblankenship profile image
AmyBlankenship • Edited

So, if your data structure is

[{firstName: 'John', lastName: 'Smith', offspring: [{name:'Katie', gender: 'F'}, {name: 'Mark', gender: 'M'}],
{firstName: 'Jill', lastName: 'Jones', offSpring: [{name: 'Doug', gender: 'M'}]}]

and you're looking for someone whose firstborn is a son (in case of pestilence), you can do _.find(users, ['offspring[0].gender', 'M'])

Or if you want the last name of the second user, you can do _.get(users, '[1].lastName')

Or if you want to find all users with offspring
_.filter(users, 'offspring.length')

I just don't see easily being able to do that with native ES6 functions out of the box.

Thread Thread
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited
const arr = [{
    firstName: 'John',
    lastName: 'Smith',
    offspring: [{
        name:'Katie', 
        gender: 'F'
    }, {
        name: 'Mark',
        gender: 'M'
    }],
    {
        firstName: 'Jill', 
        lastName: 'Jones', 
        offSpring: [{
            name: 'Doug', 
            gender: 'M'
        }]
    }
];

arr.find(person => person.offspring.find ... // Point taken this is a bit more wordy😁
Thread Thread
 
amyblankenship profile image
AmyBlankenship

So in other words, you can't do it out of the box with ES6 functions--you have to write additional functionality yourself (which your team is then responsible for testing and maintaining).