Javascript 有没有办法用normalizer保持递归子级顺序?
我正在从服务器检索排序后的注释数组。每个注释都有一个Javascript 有没有办法用normalizer保持递归子级顺序?,javascript,reactjs,redux,normalization,normalizr,Javascript,Reactjs,Redux,Normalization,Normalizr,我正在从服务器检索排序后的注释数组。每个注释都有一个comment.children属性,该属性也是其他注释的排序数组。这些可以嵌套ndeep。例如: const nestedComments = [ { id: 1, body: "parent comment", children: [ { id: 4, body: 'child comment', children: [
comment.children
属性,该属性也是其他注释的排序数组。这些可以嵌套n
deep。例如:
const nestedComments = [
{
id: 1,
body: "parent comment",
children: [
{
id: 4,
body: 'child comment',
children: [
{
id: 7,
body: 'grandchild comment #1',
children: [],
},
{
id: 6,
body: 'grandchild comment #2',
children: [],
},
{
id: 5,
body: 'grandchild comment #3',
children: [],
},
]
},
{
id: 3,
body: 'second child comment',
children: []
}
],
},
{
id: 8,
body: 'parent comment #2',
children: []
},
];
然后我使用normalizer库对其进行规范化,如下所示:
const commentSchema = new schema.Entity('comments');
const commentListSchema = new schema.Array(commentSchema);
commentSchema.define({children: commentListSchema});
const normalizedComments = normalize(nestedComments, commentListSchema);
results: [[1,8], [4,3], [7,6,5]];
结果与预期基本一致:
{
entities: {
// All of the comments ordered their id's, this is fine and what I want
},
results: [1,8] // understandable but not what I want
}
因此,它保留根注释的顺序,但不对嵌套的子注释执行任何操作。是否有办法做到这一点,使每组兄弟姐妹都有自己的结果
数组?看起来像这样的东西:
const commentSchema = new schema.Entity('comments');
const commentListSchema = new schema.Array(commentSchema);
commentSchema.define({children: commentListSchema});
const normalizedComments = normalize(nestedComments, commentListSchema);
results: [[1,8], [4,3], [7,6,5]];
或者也许有更好的方法来保存这些信息,我也希望听到这些信息。结果中已经给出了对我有用的答案。实体中的注释按原始顺序存储子ID数组
entities: {
{
id: 1,
body: 'parent comment',
children: [4,3]
},
...
{
id: 4,
body: 'child comment',
children: [7,6,5],
},
...
}