codesnippet(持续更新中...)

记录一些有用的代码片段

javascript

  • 比较一个大的对象中的除了某一个子对象的其他对象是否相等
    1
    2
    3
    4
    5
    6
    7
    8
    componentWillReceiveProps (nextProps) {
    const staticNextProps = lodash.cloneDeep(nextProps)
    delete staticNextProps.columns
    const { columns, ...otherProps } = this.props
    const isEqual = lodash.isEqual(staticNextProps, otherProps));
    ......
    }

点评
很巧妙的在不破坏原数据(nextProps)的情况下深拷贝出来一个对象staticNextProps,因为不需要比较columns这个子对象,然后删除这个子对象,最后更巧妙地是使用ES6的rest运算符…otherProps将this.props中除了cloumns子对象的其他对象包装成一个新的对象,最后使用lodash的isEqual深比较方法进行比较,非常犀利的思路!
此段代码来自antd-admin示例代码