All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2701_2800.s2705_compact_object.solution.ts Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
// #Medium #2023_07_29_Time_78_ms_(99.38%)_Space_53.4_MB_(71.88%)

type Obj = Record

function compactObject(obj: Obj): Obj {
    if (Array.isArray(obj)) {
        let retArr = []
        obj.forEach((e, idx) => {
            if (e) {
                retArr.push(compactObject(e))
            }
        })
        return retArr
    } else if (obj !== null && typeof obj === 'object') {
        let retObj = {}
        for (const key of Object.keys(obj)) {
            if (obj[key]) {
                retObj[key] = compactObject(obj[key])
            }
        }
        return retObj
    }
    return obj
}

export { compactObject }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy