It's pronounced Diane. I do data architecture, operations, and backend development. In my spare time I maintain Massive.js, a data mapper for Node.js and PostgreSQL.
includes returns a boolean value but forEach does not (or rather, it returns undefined). So the result of the includes call is simply thrown away if you use forEach instead of some.
Two obvious improvements to the working method:
iterate restaurant.menuEntities with some as well, if it's possible that the result you're looking for is not in the very first one
use reduce instead of filter; if you search, there have been numerous guides posted here
Additionally, if you're retrieving the restaurant & menu records from a relational database you could short-circuit the whole thing by filtering in your query instead of in application logic.
Hahhaa for the menuEntities yeah I'll definitely have to use .some if there's more than one.. Thanks for the reminder & explanation, makes sense that the results of includes is thrown away with forEach
It's pronounced Diane. I do data architecture, operations, and backend development. In my spare time I maintain Massive.js, a data mapper for Node.js and PostgreSQL.
The reduce accumulator doesn't have to be one value -- it's quite useful in places you'd use both filter and map to choose some elements of an array and generate derived values, and you only have to traverse the array once.
Looks like the answer to why your initial solution didn't work has been answered. I just wanted to leave a few suggestions.
Avoid using == in favor of ===. == Will attempt to type convert and unless that's what you need you can run into some false positives. If it is what you explicitly need you're better off converting and then comparing with === yourself
Instead of checking _.isEmpty(thing) == false, you can write !isEmpty(thing). It'll be easier to read when/if you or anyone else has to come back and do additional work on this code.
You're mixing two flavors of syntax with an ES5 function definition and an ES6 fat arrow function definition. It's not wrong but unless you have a good reason for it, it's probably better to stick to one again for code legibility down the line.
Hi, a filter must return boolean for every element in the array, you are not doing that. The filter will fill your result with only the elements that returned true. You should check the mdn docs for the Array.prototipe.filter, it comes with examples and very well explained.
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
Don't forget filtter and find. Find gives just one result and filter gives many reults.
If I'm looping through a crowd of people to find on person.
If I'm looking for a particular group to filter through.
Pellentesque nec neque ex. Aliquam at quam vitae lacus convallis pulvinar. Mauris vitae ullamcorper lacus. Cras nisi dui, faucibus non dolor quis, volutpat euismod massa. Donec et pulvinar erat.
includesreturns a boolean value butforEachdoes not (or rather, it returnsundefined). So the result of theincludescall is simply thrown away if you useforEachinstead ofsome.Two obvious improvements to the working method:
restaurant.menuEntitieswithsomeas well, if it's possible that the result you're looking for is not in the very first onereduceinstead offilter; if you search, there have been numerous guides posted hereAdditionally, if you're retrieving the restaurant & menu records from a relational database you could short-circuit the whole thing by filtering in your query instead of in application logic.
Hahhaa for the menuEntities yeah I'll definitely have to use .some if there's more than one.. Thanks for the reminder & explanation, makes sense that the results of includes is thrown away with forEach
Just to summarize:
forEachif you need to act on every item (display all prices)mapsame, but return a new array with the new values (add 10% tax to all prices)filterif you need to remove items (get prices under 10$)includesif you need to know if the array contains at least one (does one prices is exactly equal to 10$)somesame, but you can use a function as assertion (does one price is under 10$)reduceif you need to transform an array into one value (sum all prices)ps: Try to never modify a variable (except iterator).
The
reduceaccumulator doesn't have to be one value -- it's quite useful in places you'd use bothfilterandmapto choose some elements of an array and generate derived values, and you only have to traverse the array once.For large data set I would agree (even tho I never thought of it). But for sub 100 items, the lose in clarity isn't much worth it.
Looks like the answer to why your initial solution didn't work has been answered. I just wanted to leave a few suggestions.
Avoid using == in favor of ===. == Will attempt to type convert and unless that's what you need you can run into some false positives. If it is what you explicitly need you're better off converting and then comparing with === yourself
Instead of checking _.isEmpty(thing) == false, you can write !isEmpty(thing). It'll be easier to read when/if you or anyone else has to come back and do additional work on this code.
You're mixing two flavors of syntax with an ES5 function definition and an ES6 fat arrow function definition. It's not wrong but unless you have a good reason for it, it's probably better to stick to one again for code legibility down the line.
Hi, a filter must return boolean for every element in the array, you are not doing that. The filter will fill your result with only the elements that returned true. You should check the mdn docs for the Array.prototipe.filter, it comes with examples and very well explained.
Don't forget filtter and find. Find gives just one result and filter gives many reults.
If I'm looping through a crowd of people to find on person.
If I'm looking for a particular group to filter through.
Don't use
forEach. You're expecting the end result to be an array. Use.map,.filter, or.reducewith to return a new array.github.com/mihaifm/linq and jquery grep are helpful.
You may also want to take a look at underscorejs.org, it's a lightweight library that makes things like this much easier and cleaner
aye i already am using underscorejs, installed lodash in my project. what would you suggest to get this done though?
I think you have gotten enough suggestions by now to 'wrap your head around it'.
@sarah_edo wrote a cool tool, Array Explorer, that I think you'd find helpful.
codepen.io/sdras/details/gogVRX
This is awesome & definitely useful! Thanks for the intro Nick and ofc thanks Sarah for making this!
If it helps, here's my writeup of Array methods in JavaScript:
What to do with lists of things in JavaScript
Klemen Slavič