多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ### 防抖: ~~~ import debounce from 'lodash/debounce'; const _handleSearch = debounce(handleSearch, 300); ~~~ ### 深拷贝 ~~~ import lodash from 'lodash'; const objCopy = lodash.cloneDeep(obj); ~~~ ### 获取对象部分属性 ~~~javascript var object = { 'a': 1, 'b': '2', 'c': 3 }; _.pick(object, ['a', 'c']); // => { 'a': 1, 'c': 3 } ~~~ ### 获取多层对象里的值 ~~~javascript const isUpdate = get(this.props, 'location.query.action') === 'UPDATED'; ~~~ ### 点击复制(copy-to-clipboard) ~~~ import copy from 'copy-to-clipboard'; <Button type="primary" onClick={() => { copy(dewValue || ''); message.success('复制成功'); }}>复制</Button> ~~~ ### 再return中使用switch case ~~~ render(){ return {( ()=>{ switch(this.state.color){ case "1":return ; break; case "2":return ; break; case "3":return ; break; default:return null; } } )()} } ~~~ ### 对象数组中获取相同元素的值放在一个数组中 ~~~ arrs.map(item => item.name) ~~~ ### 使用color: inherit;去除a链接颜色,里面的元素颜色不变 ### 同步循环 使用for-of可以实现同步调用异步请求,但是不能异步遍历对象要通过遍历对象的值来实现(Object.values(value)) ![](https://img.kancloud.cn/e4/bd/e4bd68a17745502643bbbcd60c57bbcb_1866x422.png) ~~~ //报错 for await (const item of value) { let temp = ''; let standardValue = ''; let key = ''; switch (value[item]) { case '混合基金': temp = 'blend_bond'; standardValue = '20%沪深300+80%中证全债'; key = 'BLEND'; break; case '股票基金': temp = 'stock'; standardValue = '沪深300'; key = 'STOCK'; break; case '全量': temp = 'total'; standardValue = '沪深300'; key = 'TOTAL'; break; case '债券基金': temp = 'pure_bond'; standardValue = '中证全债'; key = 'BOND'; break; default: temp = ''; standardValue = ''; key = ''; break; } const res = await fetchPerformance({ classicalValue: temp, managerCode, managerName, standardValue, isFullValue: '_fill_nv' }); const { incomeData = [] } = res?.result; const val = incomeData.find(ele => ele.incomeIndex === '年化收益'); const stock = val.HS300 ? val.HS300.toFixed(2) : ''; const valueName = val.HS300 && val.HS300 && (val.manager > val.HS300 ? '跑赢' : '跑输') || '-'; datas[key] = { manager: val.manager ? (val.manager * 100).toFixed(2) : '', stock, value: valueName, standardValue }; } //正确写法 for await (const item of Object.values(value)) { let temp = ''; let standardValue = ''; let key = ''; switch (item) { case '混合基金': temp = 'blend_bond'; standardValue = '20%沪深300+80%中证全债'; key = 'BLEND'; break; default: temp = ''; standardValue = ''; key = ''; break; } const res = await fetchPerformance({ classicalValue: temp, managerCode, managerName, standardValue, isFullValue: '_fill_nv' }); const { incomeData = [] } = res?.result; const val = incomeData.find(ele => ele.incomeIndex === '年化收益'); const stock = val.HS300 ? val.HS300.toFixed(2) : ''; const valueName = val.HS300 && val.HS300 && (val.manager > val.HS300 ? '跑赢' : '跑输') || '-'; datas[key] = { manager: val.manager ? (val.manager * 100).toFixed(2) : '', stock, value: valueName, standardValue }; } ~~~ ### 中文排序localeCompare ~~~ sorter: (a, b) => { if (a.fundManagerName && b.fundManagerName) { return a.fundName.localeCompare(b.fundName); } }, ~~~ ### 下拉框懒加载 ~~~ <Select value={this.state.value} placeholder="请输入关键字" showSearch filterOption={false} onPopupScroll={(e)=>{ const { target } = e; // target.scrollTop + target.offsetHeight === target.scrollHeight 判断是否滑动到底部 if( target.scrollTop + target.offsetHeight === target.scrollHeight){ // 在这里调用接口 } }} > {list.map(item) => <Option key={item.id}>{item.name}</Option>} </Select> ~~~ ### lodash中的过滤(filter) ~~~ lodash.filter([],e=>{}) ~~~ ### lodash中数组去重(uniqBy) ~~~ lodash.uniqBy( lodash.filter( typeof serviceList === 'object' ? serviceList : [], item => item.bussinessCode === this.state.bussinessCode, ), 'businessCode', ) ~~~ ### 对象遍历(Object.entries) ~~~ Object.entries( lodash.pick( this.state, formConfig.map(item => item.key), ), ) ~~~ ### lodash中判断(some)任意元素(数组,对象,函数)经函数检查是否满足调整 ~~~ const disabled = lodash.some( Object.entries( lodash.pick( this.state, formConfig.map(item => item.key), ), ).map(item => { // 策略类型为元数据类型的时候,关联策略 code 不需要填 if ( item[0] === 'releatedStrategyCode' && disableRelativeStrategyCode.includes(this.state.definitionType) ) { return true; } // if ( // item[0] === 'bizParam' && // !showNeedSelectByDefault.includes(this.state.definitionType) // ) { // return true; // } // if ( // item[0] === 'dataSourceList' && // !showNeedSelectByDefault.includes(this.state.definitionType) // ) { // return true; // } if (item[0] === 'needCompleteByDefault') { return true; } return item[1]; }), item => !item, ); ~~~ ### js中的some判断数据中的是有有达到条件的值 ~~~ 检测数组中是否有元素大于 18: var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.some(checkAdult); } ~~~ ### 下拉框带搜索功能 ~~~ const filterFunction = (input: string, option: any) => { const children = option.props?.children || ''; const childrenString = (Array.isArray(children) ? children.join('').toLowerCase() : children?.toLowerCase()) || ''; const value = option.props?.value?.toLowerCase() || ''; const lowerInput = input.toLowerCase(); return value.indexOf(lowerInput) >= 0 || childrenString.indexOf(lowerInput) >= 0; }; export const searchAbleProps = { showSearch: true, filterOption: filterFunction, }; <Select {...searchAbleProps} className={styles.right} onChange={this.onSelectChange('bizParamType')} placeholder="选择选品类型" value={bizParamType} > {bizParamTypeList.map(item => ( <Option key={item.selectionId} value={item.code}> {item.name} </Option> ))} </Select> ~~~ ### 点击复制 ~~~ "clipboard-copy": "^3.1.0", import copy from 'clipboard-copy'; import { CopyTwoTone } from '@ant-design/icons'; { title: '展示单元id', key: 'id', // width: 200, render: ({ id }) => { return <div style={{ fontSize: '14px' }} onClick={() => { copy(id); message.success('复制成功'); }}>{id || '--'}&nbsp; <CopyTwoTone /></div>; }, }, ~~~ ### antd 3.x 表单去除空格 ~~~ <Form.Item label="描述"> {getFieldDecorator('desc', { rules: [{ required: true, message: '请输入描述信息!' }], // getValueFromEvent: value => value.target.value.replace(/^\s\s*/, '').replace(/\s\s*$/, ''), getValueFromEvent: value => value.target.value.trim(), })(<Input disabled={detail} placeholder="请输入" />)} </Form.Item> ~~~ ### 下载表格 ~~~ 安装 npm install js-export-excel import React, { Component } from 'react' import ExportJsonExcel from 'js-export-excel'; export default class Show extends Component { editor = () => { const option = {}; // 文件名字 option.fileName = 'excel'; // 文件的数据集 option.datas = [ { // 通常用来放我们的表格数据 sheetData: [ { name: "hig girl", old: 12 }, { name: "you boy", old: 20 }, ], // sheet名字 sheetName: 'sheet', // 第一行 sheetHeader: ['名字', '年龄'], //列宽 需与列顺序对应, 需要则写 // columnWidths: [] } ] const toExcel = new ExportJsonExcel(option); let file = toExcel.saveExcel() } render() { return ( <div> show <button onClick={this.editor}>下载</button> </div> ) } } ~~~