I have an array of arrays like this
let a = [
// Line # Vendor Proj Amount Description
[ '1', '213e', '2300', '456.09', 'for gutter cleaning'],
[ '2', '3334', '3321', '321.10', 'for upkeep'],
[ '3', '213e', '2300', '456.09', 'for mowing']
]
I’d like to filter the array to only show the arrays that have the same vendor, project and amount. How can I do that in an elegant way, without using a nested loop?
I’m wondering if I can find dupes with a combination of reduce and filter? I know this isn’t in the standard, but are there any homebrewed solutions?
Something like
let dupes = a.reduceFilter((curArr, nextArr) => curArr[1] === nextArr[1] && curArr[2] === nextArr[2] && curArray[3] === nextArr[3])
And then I’d end up with this:
dupes: [
['1','213e','2300','456.09','for gutter cleaning'],
['1','213e','2300','456.09','for mowing']
]
5
Answers
This is not the ideal solution, but it's the best I have come up with:
What I'd really like to do is abstract this behind a prototype.
Consider grouping the items and then filter groups bigger than one
I do not ever recommend using this in production but here is my solution in a single line. Basically, use a filter to check if the rest of the items concatenated into a single string contain the vendor, project, and amount as a single string. If so, it is a dupe
I would use reduce and flatMap to get the dupes.
The code below is self-documenting.
I broke each step of the logic out into individual functions.