问题描述
我有一个包含第一级项目的数组。例如:
Folder1
Folder2
Folder3
Page
Page
每个人都有一个ID。要获取该数据,我使用graphql查询:
async loadBrowseData (){
this.$store.commit(`loadingStart`,'browse-load')
const resp = await this.$apollo.query({
query: gql`
query ($parent: Int!,$locale: String!) {
pages {
tree(parent: $parent,mode: ALL,locale: $locale,includeAncestors: true) {
id
path
title
parent
isFolder
depth
pageId
parent
locale
}
}
}
`,fetchPolicy: 'cache-first',variables: {
parent: 0,locale: 'en'
}
})
this.staticData = _.get(resp,'data.pages.tree',[])
如您所见,它使用Lodash进行数组操作。
现在,我已经获得了属于特定文件夹的项目。它正在使用另一个graphql查询,该查询基于父ID获取特定文件夹的文件。看看:
async fetchData (item) {
const resp = await this.$apollo.query({
query: gql`
query ($parent: Int,$locale: String!) {
pages {
tree(parent: $parent,locale: $locale) {
id
path
title
isFolder
pageId
parent
locale
}
}
}
`,variables: {
parent: 3,locale: this.locale
}
})
this.childData = _.get(resp,[])
如您所见,我有两个查询执行两项工作。第一个是获取第一级项目,第二个是根据文件夹的父ID返回属于特定文件夹的项目。
所以最终,我得到了这样的东西:
Folder1 ¬
SubFolder
Page
Folder2 ¬
Page
Folder3
Page
Page
我想要实现的是,我想将所有这些结构作为单个JS对象:
Folder1:{
Subfolder:{},Page:{}
},Folder2: {
Page:{}
},Folder3:{},Page:{},Page:{}
这个想法是我目前有2个数组。对于第一级项目,第二个数组是包含每个父文件夹文件的子节点。
如何结合这两个数组,以便构建上面显示的结构?
该项目的主要开发人员为此使用了Lodash。有没有办法做到这一点?还是可以用纯Javascript完成?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)