Я хотел бы применить компактный метод 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 возвращал массив, который исключил бы все ключи / значения, где значение равно нулю. Как это:
[{
"_id": "5aa98490e0e1b4045a67f1d5",
"type": "static01",
"parentLesson": {
"_id": "5aa946f2e6d64f5180c8e7ce"
}
},
{
"_id": "5aa9a9c9175873221ca26d66",
"name": "Lesson01-Page02 JP"
"parentLesson": {
"_id": "5aa946f2e6d64f5180c8e7ce"
}
}]
Как я могу этого добиться?



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Это самая странная вещь, которую я сделал в 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.info(newData);<script src = "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>@pedrosimao => У вас есть обновленная структура объектов, которую я мог бы протестировать? Это работает для этого конкретного набора данных, но может возникнуть что-то, что я не обрабатываю.
Конечно, вот точный пример, который я использовал: drive.google.com/open?id=11j7uI3V6Gw2-TdPROtunvW_Iuqm8zZDn
@pedrosimao => Я добавил ваш набор данных из этого файла в свой код выше, и он отлично работает. В вашем коде должно быть что-то еще, что вызывает проблему с переполнением стека.
Действительно, это выглядит супер безумно. Я вижу, у вас есть мастерство лодаша. Он отлично работает в Code Snipet, но на моем сервере я получил ошибку «Превышен максимальный размер стека вызовов». Я не уверен, смешно это или страшно, поскольку у меня должно быть достаточно стека, чтобы иметь дело с гораздо более крупными объектами, чем тот, который я использовал.