post: {
comments: {
1: {
id: 1,
parent: null
},
2: {
id: 2,
parent: 3
},
3: {
id: 3,
parent: 1
},
4: {
id: 4,
parent: null
}
}
}
parent: null
?post: {
comments: {
1: {
id: 1,
parent: null
},
4: {
id: 4,
parent: null
}
}
}
let n = {}
for (i in post.comments) {
if (post.comments[i].parent === null) {
Object.assign(n, {[i]: post.comments[i]})
}
}
Object.entries(comments).reduce((acc, [ k, v ]) => v.parent === null ? { ...acc, [k]: v } : acc, {})
Object.values(comments).reduce((acc, n) => (n.parent === null && (acc[n.id] = n), acc), {})