Filtering object properties based on value

前端 未结 7 773
孤独总比滥情好
孤独总比滥情好 2020-12-14 16:40

Is there some elegant way of filtering out falsey properties from this object with lodash/underscore? Similar to how _.compact(array) removes falsey elements fr

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 17:08

    Another approach

    const objFilter = (obj, condition) => {
        let newObj = {}
        for (const [key, value] of Object.entries(obj)) {
            if (condition(value)) {
                newObj = { ...newObj, [key]: value }
            }
        }
        return newObj
    }
    

    Fire like this:

    const newData = objFilter(oldData, (value) => value.marked === false)
    

提交回复
热议问题