Comments (3)
I too have made this helper myself! but called it countIf. nWhere is nicer I think, doesn't conflict with the summarizeIf style of things... my implementations avoided creating another array by using sum + predicate, more SQL style in a way. I'd be happy to add these as nWhere
and sumWhere
if you're up for it!
export function countIf<T extends object>(predicate: (d: T) => boolean) {
return (items: T[]) => sum((d: T) => (predicate(d) ? 1 : 0))(items)
}
export function sumIf<T extends object>(
predicate: (d: T) => boolean,
key: keyof T | ((d: T) => number)
) {
const keyFn =
typeof key === 'function' ? key : (d: T) => d[key] as unknown as number
return (items: T[]) => sum((d: T) => (predicate(d) ? keyFn(d) : 0))(items)
}
from tidy.
Sounds good – go for it!
from tidy.
I actually ended up doing this as an option on sum and n now which matches a couple other places, available in v2.5.0.
const data = [
{ str: 'foo', value: 3 },
{ str: 'foo', value: 1 },
{ str: 'bar', value: 3 },
{ str: 'bar', value: 1 },
{ str: 'bar', value: 7 },
];
tidy(data, summarize({
count: n(),
countFoo: n({ predicate: d => d.str === 'foo' })
})
// output:
[{ count: 5, countFoo: 2 }]
from tidy.
Related Issues (20)
- Clarity on dependencies? HOT 2
- Bug: groupBy exports do not respect addGroupKeys
- Bug: mapEntry not called on entriesObject
- valuesFillMap for pivotWider() not being applied HOT 2
- Feedback on unexpected behavior in groupBy HOT 9
- Should groupBy's addGroupKeys be on by default?
- Allow type guards for filter HOT 2
- leftJoin followed by select everything has unexpected dropped columns
- Typescript type error? HOT 1
- Mutate should pass the index
- GroupBy fails with Dates HOT 1
- arrange should accept accessors
- Update docs to reflect keys being prepended now HOT 1
- Feature Request: add a boolean flag to turn on/off debug HOT 1
- New Architecture
- Type error when using groupBy after mutate HOT 2
- Set colum index for mutations HOT 7
- complete, is not parsing the original data HOT 1
- import fail HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tidy.