Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 父数组和子数组排序不';我不能据此工作_Javascript_Typescript - Fatal编程技术网

Javascript 父数组和子数组排序不';我不能据此工作

Javascript 父数组和子数组排序不';我不能据此工作,javascript,typescript,Javascript,Typescript,我有树节点列表metadataList如下: [ { "data": { "metadata": { "category": [ "Csp" ] } }, "children": [ { "data": { "metadata": { "category": [ "Csp"

我有树节点列表
metadataList
如下:

   [
  {
    "data": {
      "metadata": {
        "category": [
          "Csp"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Csp"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  },
  {
    "data": {
      "metadata": {
        "category": [
          "Isv"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Isv"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Isv"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  },
  {
    "data": {
      "metadata": {
        "category": [
          "Csp"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Csp"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  },
  {
    "data": {
      "metadata": {
        "category": [
          "Mpn"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Csp"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Isv"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  },
  {
    "data": {
      "metadata": {
        "category": [
          "Incentives"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Incentives"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  }
]
export default class CurrentTopicMetadataTreeNode {
    public data: CurrentTopicMetadata;
    public children: CurrentTopicMetadataTreeNode[];
}

export default class CurrentTopicMetadata {
    public id: string;
    public metadata: TopicMetadata 

}

export class TopicMetadata {
    public category: Category[] 

}

export enum Category {
    Csp = 'Csp',
    Mpn = 'Mpn',
    Incentives = 'Incentives',
    Referrals = 'Referrals',
    Isv = 'Isv',

}
这是一种类型的
数据
子对象
集合数组,其类如下:

   [
  {
    "data": {
      "metadata": {
        "category": [
          "Csp"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Csp"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  },
  {
    "data": {
      "metadata": {
        "category": [
          "Isv"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Isv"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Isv"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  },
  {
    "data": {
      "metadata": {
        "category": [
          "Csp"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Csp"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  },
  {
    "data": {
      "metadata": {
        "category": [
          "Mpn"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Mpn"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Csp"
            ]
          }
        },
        "children": [

        ]
      },
      {
        "data": {
          "metadata": {
            "category": [
              "Isv"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  },
  {
    "data": {
      "metadata": {
        "category": [
          "Incentives"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Incentives"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  }
]
export default class CurrentTopicMetadataTreeNode {
    public data: CurrentTopicMetadata;
    public children: CurrentTopicMetadataTreeNode[];
}

export default class CurrentTopicMetadata {
    public id: string;
    public metadata: TopicMetadata 

}

export class TopicMetadata {
    public category: Category[] 

}

export enum Category {
    Csp = 'Csp',
    Mpn = 'Mpn',
    Incentives = 'Incentives',
    Referrals = 'Referrals',
    Isv = 'Isv',

}
我正在尝试的是,根据类别将列表筛选为
数据
子项
顺序。假设按类别筛选,则所有数据和子项都属于该类别,其顺序如下

但我得到的数据如下:

数组问题集中的一个元素:

在这个数组中,如果我用
Csp
搜索,则根节点中只有
Csp
的数据,而
子节点中的数据则只有一个
数据,其中包含
Csp

[{
    "data": {
      "metadata": {
        "category": [
          "Csp"
        ]

      }


    },
    "children": [
      {
        "data": {

          "metadata": {
            "category": [
              "Csp"
            ]

          }



        },
        "children": [

        ]
      },
      {
        "data": {

          "metadata": {
            "category": [
              "Mpn"
            ]
          }



        },
        "children": [

        ]
      },
      {
        "data": {

          "metadata": {
            "category": [
              "Mpn"
            ]
          }

        },
        "children": [

        ]
      },
      {
        "data": {

          "metadata": {
            "category": [
              "Mpn"
            ]

          }

        },
        "children": [

        ]
      }
    ]
  }]
预期输出:
因此,经过
Csp
过滤后,节点应如下所示:

[
  {
    "data": {
      "metadata": {
        "category": [
          "Csp"
        ]
      }
    },
    "children": [
      {
        "data": {
          "metadata": {
            "category": [
              "Csp"
            ]
          }
        },
        "children": [

        ]
      }
    ]
  }
]
这是我的代码,我哪里做错了

//规则1检查父元数据类别是否为空
//规则2和3
函数查找子项中的子项(子项、父项类别){
儿童有相同的类别=[]
for(儿童中的变量i){
让孩子=孩子[我];
if(child.children!=未定义&&child.children.length>0&&child.data.metadata.category==parent\u category){
children\u具有相同的\u类别。push(child);
}
}
如果(子项具有相同的类别长度>0){
返回子项\u具有\u相同\u类别
}否则{
for(儿童中的变量i){
让孩子=孩子[我];
返回查找子项中的子项(child.children,parent\u类别);
}
}
}
功能检查对象(对象){
让parent_category=object.data.metadata.category[0];
如果(object.children!=未定义&&object.children.length>0){
返回{'data':object.data,'children':在\u children(object.children,parent\u category)中查找\u}
}否则{
返回{'data':object.data}
}
}
函数应用规则(对象){
//规则1检查父元数据类别是否为空
if(object.data.metadata.category.length>0){
返回{'data':object.data}
}否则{
返回检查对象(对象)
}
}
目标={
值:“Isv”
}
过滤的_数据=[]
用于(数据中的var i){
设数据=数据[i];
如果(data.data.metadata.category.length>0){
结果=应用规则(数据)
if(result.data.metadata.category[0]==target.value){
过滤数据推送(结果);
}
}

}
您可以使用

const data=[{“data”:{“category”:[“Csp”]},children:[{“data”:{“category”:[“Csp”]},children:[]},{“data”:{“metadata”:{“category”:[“Mpn”},children:[]},{“data”:{“category”:[“Mpn”},children,“子项:{“数据”:{“元数据”:{“类别”:{“Isv”]},子项:{“数据”:{“元数据”:{“类别”:{“Isv”]},子项:{“数据”:{“元数据”:{“类别”:{“Csp”]},子项:{“数据”:{“元数据”:{“类别”:{“Csp”},子项:{“数据”:{“元数据”:{“子项”},子项:{“元数据”:{“类别”:{“Mpn”},子项”{“元数据”:{},{“数据”:{“元数据”:{“类别”:[“Mpn”]},子项:[]},{“数据”:{“元数据”:{“类别”:[“Mpn”]},子项:[]},{“数据”:{“元数据”:{“类别”:[“Csp”]},子项:[]},{“数据”:{“元数据”:{“类别”:[“Isv”]},子项[]},{“数据”:{“元数据”:{“类别”:{“奖励”},子项[]},{子项“:[]}]}]
常量dfs=(索引节点,类型)=>{
const node=Object.assign({},iNode)//浅拷贝当前节点
node.children=iNode.children.flatMap(child=>{
//若子项匹配类型,则返回它,否则将其过滤掉
返回child.data.metadata.category.includes(类型)?dfs(child,类型):[]
})
返回节点
}
//伪造根节点以在其上应用dfs
const cspList=dfs({children:data},'Csp')。children

console.log(JSON.stringify(cspList,null,2))
您可以使用递归调用reduce筛选数据:

const filterItems = (items, f) => {
  const fitems = items.reduce((acc, rec) => {
      const children = rec.children.length > 0 ? filterItems(rec.children, f): []
      if (rec.data.metadata.category.indexOf(f) >= 0) {
        return [...acc, {...rec, children}]
      }
      return [...acc]
  }, [])
  return fitems
}

我应该怎么做?删除对我们的问题“namePath”、“reportingProblem”等没有意义的所有属性。显示一个示例输出,不是在图片中,而是在文本中(在您的图片中,我们不知道数据和子项的内容,所以我们不知道您过滤了什么)根据您的建议更新了问题。希望现在可以阅读。我根据对问题的理解进行了回答。排序和排序显示在您的帖子中,因此我不确定您是否要筛选出与您的类型不匹配的节点,或者是否要先用与您的类型匹配的节点对列表进行排序(您删除了用于标识节点的id,这有点过于激进了)谢谢你的回答,在第一种方法中是的,但在下一种递归方法中,我尝试过滤符合我搜索键的节点。仅。我也在检查你的节点。再次感谢。你能告诉我我的错误是什么吗?没有什么问题。我非常接近。很好的发现,我没有想到礼帽。我在玩弄你的答案。在这一行中
返回child.data.metadata.category.includes(type)?dfs(child,type):[]
一旦类别为空,您就放置了一个空数组,这将创建一个
[]
当我尝试使用
激励措施时,输出上的数组列表
我无法将其从列表中删除。有其他方法可以删除它吗?我已将其替换为
'
,然后像
const arrperted=cspList.filter(el=>{return el!=null&&el!=''>})一样对其进行过滤。
。如果输出为[]这意味着没有一个孩子符合我们想要的激励?如果您对flatMap不满意,您可以使用经典的过滤器:
v=children.filter(c=>c.data.metadata.category.includes(type))
,它只保留您的孩子