How best to convert a ClientRect / DomRect into a plain Object

后端 未结 5 1036
遇见更好的自我
遇见更好的自我 2020-12-20 11:00

The result of someElement.getBoundingClientRect() returns a special object of type ClientRect (or DomRect apparently)

5条回答
  •  遥遥无期
    2020-12-20 12:07

    Functional ES6 variant:

    const propValueSet = (prop) => (value) => (obj) => ({...obj, [prop]: value})
    const toObj = keys => obj => keys.reduce((o, k) => propValueSet(k)(obj[k])(o), {})
    const getBoundingClientRect = el => toObj(['top', 'right', 'bottom', 'left', 'width', 'height', 'x', 'y'])(el.getBoundingClientRect())
    

提交回复
热议问题