嵌套对象的高效过滤:Lodash 或 plain JavaScript
Efficient Filtration of nested Object: Lodash or plain JavaScript
我是 lodash 的新手。我有一个 javascript 对象,如下所述。这样做的目的是 explore/learn 对象的高效过滤过程,特别是当数据巨大且嵌套时。感谢您在这方面的指导。
JavaScript 对象是:
{
"Bus-1": {
"Seat1": {
"Bookings": {
"21032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Jon Doe"
},
"22032022": {
"BookedAt": "21/03/2022 9:43 PM",
"BookedBy": "James"
}
},
"Id": 1
},
"Seat2": {
"Bookings": {
"20032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Elijah"
},
"21032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Scott"
}
},
"Id": 2
},
"Seat3": {
"Bookings": {
"22032022": {
"BookedAt": "22/03/2022 02:41 AM",
"BookedBy": "Williams"
}
},
"Id": 3
}
},
"Bus-2": {
"Seat1": {
"Bookings": {
"22032022": {
"BookedAt": "22/03/2022 02:39 AM",
"BookedBy": "Lucas"
}
},
"Id": 1
}
}
}
从上面的集合中,对象层次结构如下图所示。
到目前为止我尝试了什么
loop...
Object to array and then array filter function which further leads to filtration again and again
Object to array and then lodash filter function which also cause nested filtration again and again in this case
我想达到的目标:
到目前为止,我发现 当存在大量数据时,循环和过滤会减慢处理速度。
我正在寻找 一种有效的方法,它 return 基于日期 的所有预订的对象,如下所述,以便我可以进一步验证用户是否在同一天进行预订。
"21032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Jon Doe"
},
"22032022": {
"BookedAt": "21/03/2022 9:43 PM",
"BookedBy": "James"
},
"20032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Elijah"
},
"21032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Scott"
},
"22032022": {
"BookedAt": "22/03/2022 02:41 AM",
"BookedBy": "Williams"
},
"22032022": {
"BookedAt": "22/03/2022 02:39 AM",
"BookedBy": "Lucas"
}
此致,
亚克达斯
这个:
const result = _.assign(..._.flatMap(val, (seats, busKey) =>
_.flatMap(seats, (content, seatKey) =>
_.mapKeys(content.Bookings, (_, bookingKey) =>
_.join([busKey, seatKey, bookingKey], '_')))
))
输出这个:
{
Bus-1_Seat1_21032022: {
BookedAt: "21/03/2022 3:43 PM",
BookedBy: "Jon Doe"
},
Bus-1_Seat1_22032022: {
BookedAt: "21/03/2022 9:43 PM",
BookedBy: "James"
},
Bus-1_Seat2_20032022: {
BookedAt: "21/03/2022 3:43 PM",
BookedBy: "Elijah"
},
Bus-1_Seat2_21032022: {
BookedAt: "21/03/2022 3:43 PM",
BookedBy: "Scott"
},
Bus-1_Seat3_22032022: {
BookedAt: "22/03/2022 02:41 AM",
BookedBy: "Williams"
},
Bus-2_Seat1_22032022: {
BookedAt: "22/03/2022 02:39 AM",
BookedBy: "Lucas"
}
}
Step-by-step:
- 在
val
上申请flatMap
以获得相应巴士的座位和钥匙
- 在席位上应用
flatMap
以获取每个席位的内容及其密钥
- 获取预订
- 对预订应用
mapKeys
以避免最终对象发生碰撞。这就是我们提取每个级别的密钥的原因。假设一个有效的输入对象,键将是唯一的。
- 使用扩展运算符对结果使用
assign
将所有内容合并为一个对象
要反转这个过程,像这样:
const output = {}
_.forEach(_.toPairs(result), ([key, value]) => {
const keys = _.split(key, '_')
_.set(output, keys, value)
})
我是 lodash 的新手。我有一个 javascript 对象,如下所述。这样做的目的是 explore/learn 对象的高效过滤过程,特别是当数据巨大且嵌套时。感谢您在这方面的指导。
JavaScript 对象是:
{
"Bus-1": {
"Seat1": {
"Bookings": {
"21032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Jon Doe"
},
"22032022": {
"BookedAt": "21/03/2022 9:43 PM",
"BookedBy": "James"
}
},
"Id": 1
},
"Seat2": {
"Bookings": {
"20032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Elijah"
},
"21032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Scott"
}
},
"Id": 2
},
"Seat3": {
"Bookings": {
"22032022": {
"BookedAt": "22/03/2022 02:41 AM",
"BookedBy": "Williams"
}
},
"Id": 3
}
},
"Bus-2": {
"Seat1": {
"Bookings": {
"22032022": {
"BookedAt": "22/03/2022 02:39 AM",
"BookedBy": "Lucas"
}
},
"Id": 1
}
}
}
从上面的集合中,对象层次结构如下图所示。
到目前为止我尝试了什么
loop...
Object to array and then array filter function which further leads to filtration again and again
Object to array and then lodash filter function which also cause nested filtration again and again in this case
我想达到的目标:
到目前为止,我发现 当存在大量数据时,循环和过滤会减慢处理速度。 我正在寻找 一种有效的方法,它 return 基于日期 的所有预订的对象,如下所述,以便我可以进一步验证用户是否在同一天进行预订。
"21032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Jon Doe"
},
"22032022": {
"BookedAt": "21/03/2022 9:43 PM",
"BookedBy": "James"
},
"20032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Elijah"
},
"21032022": {
"BookedAt": "21/03/2022 3:43 PM",
"BookedBy": "Scott"
},
"22032022": {
"BookedAt": "22/03/2022 02:41 AM",
"BookedBy": "Williams"
},
"22032022": {
"BookedAt": "22/03/2022 02:39 AM",
"BookedBy": "Lucas"
}
此致, 亚克达斯
这个:
const result = _.assign(..._.flatMap(val, (seats, busKey) =>
_.flatMap(seats, (content, seatKey) =>
_.mapKeys(content.Bookings, (_, bookingKey) =>
_.join([busKey, seatKey, bookingKey], '_')))
))
输出这个:
{
Bus-1_Seat1_21032022: {
BookedAt: "21/03/2022 3:43 PM",
BookedBy: "Jon Doe"
},
Bus-1_Seat1_22032022: {
BookedAt: "21/03/2022 9:43 PM",
BookedBy: "James"
},
Bus-1_Seat2_20032022: {
BookedAt: "21/03/2022 3:43 PM",
BookedBy: "Elijah"
},
Bus-1_Seat2_21032022: {
BookedAt: "21/03/2022 3:43 PM",
BookedBy: "Scott"
},
Bus-1_Seat3_22032022: {
BookedAt: "22/03/2022 02:41 AM",
BookedBy: "Williams"
},
Bus-2_Seat1_22032022: {
BookedAt: "22/03/2022 02:39 AM",
BookedBy: "Lucas"
}
}
Step-by-step:
- 在
val
上申请flatMap
以获得相应巴士的座位和钥匙 - 在席位上应用
flatMap
以获取每个席位的内容及其密钥 - 获取预订
- 对预订应用
mapKeys
以避免最终对象发生碰撞。这就是我们提取每个级别的密钥的原因。假设一个有效的输入对象,键将是唯一的。 - 使用扩展运算符对结果使用
assign
将所有内容合并为一个对象
要反转这个过程,像这样:
const output = {}
_.forEach(_.toPairs(result), ([key, value]) => {
const keys = _.split(key, '_')
_.set(output, keys, value)
})