Search code examples
javascriptarrayslodash

Lodash: deep compact or compact nested object?


I would like to apply lodash compact method on all key/values of a nested object.
My array currently looks like this:

[{
    "_id": "5aa98490e0e1b4045a67f1d5",
    "name": "Lesson01-Page01 JP",
    "parentLesson": {
        "_id": "5aa946f2e6d64f5180c8e7ce",
        "childPages": null
    },
    "background": null,
    "frontImage": null,
    "animation": null
},
{
    "_id": "5aa9a9c9175873221ca26d66",
    "name": "Lesson01-Page02 JP",
    "parentLesson": {
        "_id": "5aa946f2e6d64f5180c8e7ce",
        "childPages": null
    },
    "background": null,
    "frontImage": null,
    "animation": null
}]

So basically I want lodash compact to return an array that would eliminate all key/values where value is null. Like this:

[{
    "_id": "5aa98490e0e1b4045a67f1d5",
    "type": "static01",
    "parentLesson": {
        "_id": "5aa946f2e6d64f5180c8e7ce"
    }
},
{
    "_id": "5aa9a9c9175873221ca26d66",
    "name": "Lesson01-Page02 JP"
    "parentLesson": {
        "_id": "5aa946f2e6d64f5180c8e7ce"
    }
}]

How can I achieve that?


Solution

  • This is the weirdest thing I have done in Lodash to date, but this will work for any recursively-nested object structure, no matter the depth.

    const sampleInput = [
        {
            "_id": "5aa98490e0e1b4045a67f1d5",
            "type": "static01",
            "name": "Lesson01-Page01 JP",
            "createdAt": "2018-03-14T20:22:40.769Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5aa98490e0e1b4045a67f1d5"
        },
        {
            "_id": "5aa9a9c9175873221ca26d66",
            "name": "Lesson01-Page02 JP",
            "type": "static02",
            "createdAt": "2018-03-14T23:01:29.161Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5aa9a9c9175873221ca26d66"
        },
        {
            "_id": "5aaa81a01fadff49f70c9271",
            "name": "Lesson01-Page03 JP",
            "type": "static03",
            "createdAt": "2018-03-15T14:22:24.934Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5aaa81a01fadff49f70c9271"
        },
        {
            "_id": "5ab384b46f91791a1a8e56f4",
            "name": "Lesson01-Page04 JP",
            "type": "interactive01",
            "createdAt": "2018-03-22T10:25:56.824Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5ab384b46f91791a1a8e56f4"
        },
        {
            "_id": "5ab3872b5260f41ba7c480a4",
            "name": "Lesson01-Page05 JP",
            "type": "interactive02",
            "createdAt": "2018-03-22T10:36:27.387Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5ab3872b5260f41ba7c480a4"
        },
        {
            "_id": "5ab387ce5260f41ba7c480ab",
            "name": "Lesson01-Page06 JP",
            "type": "interactive03",
            "createdAt": "2018-03-22T10:39:10.216Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5ab387ce5260f41ba7c480ab"
        },
        {
            "_id": "5ab3883f5260f41ba7c480b6",
            "name": "Lesson01-Page07 JP",
            "type": "interactive04",
            "createdAt": "2018-03-22T10:41:03.958Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5ab3883f5260f41ba7c480b6"
        },
        {
            "_id": "5ab388d25260f41ba7c480c5",
            "name": "Lesson01-Page08 JP",
            "type": "interactive05",
            "createdAt": "2018-03-22T10:43:30.048Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5ab388d25260f41ba7c480c5"
        },
        {
            "_id": "5ab38b445260f41ba7c480ce",
            "name": "Lesson01-Page09 JP",
            "type": "interactive06",
            "createdAt": "2018-03-22T10:53:56.899Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5ab38b445260f41ba7c480ce"
        },
        {
            "_id": "5ab38cbd5260f41ba7c480d5",
            "name": "Lesson01-Page10 JP",
            "type": "interactive07",
            "createdAt": "2018-03-22T11:00:13.599Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5ab38cbd5260f41ba7c480d5"
        },
        {
            "_id": "5ab3914980099f2ad11da78b",
            "name": "Lesson01-Page11 JP",
            "type": "interactive08",
            "createdAt": "2018-03-22T11:19:37.762Z",
            "parentLesson": {
                "_id": "5aa946f2e6d64f5180c8e7ce",
                "region": "5aa98ce6c6752a0dcb60d3c6",
                "childPages": null,
                "id": "5aa946f2e6d64f5180c8e7ce"
            },
            "background": null,
            "frontImage": null,
            "animation": null,
            "ballonImage": null,
            "image01": null,
            "image02": null,
            "image03": null,
            "image04": null,
            "imageDrop": null,
            "soundFile": null,
            "videoFile": null,
            "childBadges": null,
            "id": "5ab3914980099f2ad11da78b"
        }
    ];
    
    // You need to create this mixin because it needs to be called recursively.
    _.mixin({ compactObject: function(input) {
      return !_.isObject(input) ? input
        : _.chain(input)
          .keys()
          .filter(key => input[key])
          .map(key => ({
            [key]: _.isObject(input[key]) ? _.compactObject(input[key]) : input[key]
          }))
          .reduce(_.extend)
          .value();
    }});
    
    const newData = _.map(sampleInput, _.compactObject);
    
    console.log(newData);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>