import { isPlainObject } from "../../is/funcs/primitive" import { objectEach } from "./objectEach" import { setObjectValue } from "./setObjectValue" /** * 过滤对象属性 */ export function objectFilter( srcOb: any, filter: (key: string, value: any, keyPath: string[]) => boolean, options?: { deep?: boolean } ) { let newOb: any = {} if (options?.deep) { objectEach( srcOb, (value, key, { keyPath }) => { if (filter(key, value, keyPath)) { if (isPlainObject(value)) { setObjectValue(newOb, keyPath, {}) } else if (Array.isArray(value)) { setObjectValue(newOb, keyPath, []) } else { setObjectValue(newOb, keyPath, value) } } }, { needKeyPath: true } ) } else { for (let key in srcOb) { let srcValue = srcOb[key] // @ts-ignore if (filter(key, srcValue)) { newOb[key] = srcValue } } } return newOb }