javascript - Lodash:深度紧凑或紧凑的嵌套对象?

标签 javascript arrays lodash

我想对嵌套对象的所有键/值应用 lodash 紧凑方法。
我的阵列目前看起来像这样:

[{
    "_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
}]

所以基本上我希望 lodash compact 返回一个数组,该数组将消除值为 null 的所有键/值。像这样:

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

我怎样才能做到这一点?

最佳答案

这是迄今为止我在 Lodash 中做过的最奇怪的事情,但这适用于任何递归嵌套的对象结构,无论深度如何。

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>

关于javascript - Lodash:深度紧凑或紧凑的嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49757969/

相关文章:

javascript - 为什么 webpack 在使用 "import * as _"时不摇晃 lodash?

javascript - 如何在 Angular 指令中使用动态作用域名称?

javascript - 来自 Web 服务的 Angular 日期时间格式

javascript - 状态为对象数组与由 id 键控的对象

php - 基于数组键创建动态命名的变量

javascript - 如何使用 es6 或 lodash 将对象中的对象转换为数组

javascript - 如何读取指针(local_tbb.dwData)处的内存以获取窗口句柄

php - 在 MYSQL 中读取和写入 PHP 数组

c++ - 如何移动数组中的字符?

typescript - 如何安装 lodash v3 Typescript typings