如何循环通过具有子对象和数组的大对象? [英] How to loop trough a large object that has child objects and arrays?
问题描述
我正在开发一个需要实现简单搜索功能的应用程序,因此我有一个带有子对象和数组的大型对象.通常,我会像这样访问该对象中的数据:
I am developing an application where I need to implement simple search functionality, so I have this large object with child objects and arrays. Usually I access data in that object like this:
list[id][day][show].title
但是现在我需要检查标题是否等于某些输入值,所以我创建了这个函数:
but now I need to check if that title is equal to some input value, so I created this function:
getSimilarShows = (allShows, expectedShow) => {
const titles = []
Object.values(Object.values(allShows)).map((days) =>
Object.values(days).map((items) =>
Object.values(items).map((show) => {
if (show.title === expectedShow) {
titles.push(show.title)
}
})
)
)
}
这给了我一个标题数组,但我还需要ID,日期和显示保存在该数组中.
This gives me an array of titles but I also need the id, the day and the show saved in that array.
这是数据示例:
{
1: {29: [{0: {id: 0000, title: 'some title'},
{1: {id: 0000, title: 'some title'},
...],
30: [{0: {id: 0000, title: 'some title'},
{1: {id: 0000, title: 'some title'},
...],
...},
6: {29: [{0: {id: 0000, title: 'some title'},
{1: {id: 0000, title: 'some title'},
...],
30: [{0: {id: 0000, title: 'some title'},
{1: {id: 0000, title: 'some title'},
...],
...},
...}
如何正确保存它们?
推荐答案
您的数据结构并不是真正的递归.每个级别不仅代表着不同的价值(某种形式的团体,一天,也许是一个事件),而且您的结构在不同的级别上并不一致. (为什么数组位于层次结构的中间?)
Your data structure is not really recursive. Not only does each level represent a different sort of value (some sort of group, a day, perhaps an event) but your structure is not consistent at different levels. (Why the arrays in the middle of the hierarchy?)
因此递归处理不会在这里进行.但是我们可以使用以下类似方式以一种非常清晰的方式遍历结构:
So recursive processing won't do here. But we can traverse the structure in a fairly clear manner with something like this:
const getSimilarShows = (shows, title) =>
Object .entries (shows)
.flatMap (([group, days]) =>
Object .entries (days)
.flatMap (([day, events]) =>
events.flatMap ((ev) =>
Object .entries (ev)
.filter (([_, {title: t}]) => t === title)
.map (([event, {title, ...rest}]) => ({group, day, event, title, ...rest}))
)
)
)
const shows = {
1: {
29: [
{0: {id: '0001', title: 'title a'}},
{1: {id: '0002', title: 'title b'}},
],
30: [
{0: {id: '0003', title: 'title c'}},
{1: {id: '0004', title: 'title a'}},
]
},
6: {
29: [
{0: {id: '0005', title: 'title d'}},
{1: {id: '0006', title: 'title b'}},
],
30: [
{0: {id: '0007', title: 'title a'}},
{1: {id: '0008', title: 'title c'}},
]
}
}
console .log (
getSimilarShows (shows, 'title a')
)
.as-console-wrapper {max-height: 100% !important; top: 0}
我很少喜欢嵌套得如此深的代码.但是我的第一种方法是从getSimilarShows
调用getDays
调用getEvents
开始,并且在每个级别,我都必须将结果映射回到具有找到的级别键(group
,day
,event
)的对象中. )比该版本的代码更多,但仍然不够清晰.
I rarely like code that is nested so deeply. But my first approach started with getSimilarShows
calling getDays
calling getEvents
, and at each level I had to map the results back into an object with the level key found (group
, day
, event
.) It was much more code and still no more clear than this version.
说到这些组键,我必须把它们编起来.我不知道最外面的1
和6
(我称为group
)代表什么,也不知道重复的内部0
和1
(我称为event
)代表什么.我很确定29
和30
应该代表day
.因此,您可能需要更改这些属性和相应的变量.
Speaking of those group keys, I had to make them up. I don't know what the outermost 1
and 6
, which I called group
, represent, nor the repeated inner 0
and 1
, which I called event
. I'm pretty sure that 29
and 30
were supposed to represent day
s. So you may need to change those properties and the corresponding variables.
还有一个我没有命名的级别.我不太了解29
或30
的内部结构.为什么在那里有一个由单个整数键属性组成的数组,而不是像更高级别的对象那样的数组?我没有在结果中包括该索引.但是,如果您需要它,请执行以下行:
There is also a level I did not name. I don't particularly understand the structure inside, say, 29
or 30
. Why is there an array of single integer-keyed properties in there, rather than an object like the higher levels? I didn't include this index in the result. But if you need it, this line:
events.flatMap ((ev) =>
可能会变成
events.flatMap ((ev, index) =>
,您可以将index
添加到返回的对象中.
and you could add index
to the returned object.
但是,如果可以的话,我建议您调查该数组是否是必要的.
If you can, though, I would recommend looking into whether that array is even necessary.
这篇关于如何循环通过具有子对象和数组的大对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!