Typescript lodash重构滤波器函数
我有下面的代码,两个组件都使用lodash过滤器,我如何提取函数,这样我就不会重复代码Typescript lodash重构滤波器函数,typescript,refactoring,lodash,Typescript,Refactoring,Lodash,我有下面的代码,两个组件都使用lodash过滤器,我如何提取函数,这样我就不会重复代码 <VictoryScatter style={{ data: { fill: BRAND_COLORS.white, stroke: BRAND_COLORS.chartLine.gradientGrayStart, strokeWidth: 3 }, }} size={5} data={_.filt
<VictoryScatter
style={{
data: { fill: BRAND_COLORS.white, stroke: BRAND_COLORS.chartLine.gradientGrayStart, strokeWidth: 3 },
}}
size={5}
data={_.filter(loanLimitData, (value, key) => {
return key !== 0 && key !== 6;
})}
/>
<VictoryScatter
style={{
data: { fill: BRAND_COLORS.white, stroke: BRAND_COLORS.chartLine.gradientBlueStart, strokeWidth: 3 },
}}
size={5}
data={_.filter(loanBalanceData, (value, key) => {
return key !== 0 && key !== 6;
})}
/>
只需将数组作为参数:
const myFilter = <T extends any>(array: T[]): T[] =>
_.filter(array, (_, key) => {
return key !== 0 && key !== 6;
})
在您要编写的组件中:
data={myFilter(loanLimitData)}
const myFilter = <T extends any>(array: T[]): T[] =>
array.filter((_, key) => {
return key !== 0 && key !== 6;
})
data={myFilter(loanLimitData)}
data={myFilter(loanBalanceData)}