gpt4 book ai didi

javascript - 根据条件将简单数据对象转换为对象数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:22 26 4
gpt4 key购买 nike

我有一个评论/回复对象,我一直在尝试以对象数组的数组格式创建一个新对象,但它超出了我的技能水平,我目前只是一个学生

这是我要转换的对象:

[
{"id":9,"comment":"This is a normal comment to the post SON","commentername":"noforget","parentID":42,"commentVotes":0,"isWhat":"comment","voteToID":null,"votername":null,"voteAmount":null},
{"id":22,"comment":"new made comment","commentername":"noforget","parentID":42,"commentVotes":0,"isWhat":"comment","voteToID":null,"votername":null,"voteAmount":null},
{"id":23,"comment":"just let me ","commentername":"noforget","parentID":42,"commentVotes":0,"isWhat":"comment","voteToID":null,"votername":null,"voteAmount":null},
{"id":24,"comment":"bla bla","commentername":"noforget","parentID":42,"commentVotes":0,"isWhat":"comment","voteToID":null,"votername":null,"voteAmount":null},
{"id":29,"comment":"another comment","commentername":"noforget","parentID":42,"commentVotes":0,"isWhat":"comment","voteToID":null,"votername":null,"voteAmount":null},
{"id":30,"comment":"reply 12 btw","commentername":"noforget","parentID":29,"commentVotes":0,"isWhat":"reply","voteToID":null,"votername":null,"voteAmount":null},
{"id":31,"comment":"reply btw","commentername":"noforget","parentID":9,"commentVotes":0,"isWhat":"reply","voteToID":null,"votername":null,"voteAmount":null},
{"id":32,"comment":"reply to reply btw","commentername":"noforget","parentID":31,"commentVotes":0,"isWhat":"reply","voteToID":null,"votername":null,"voteAmount":null}
]

这是我希望它最终成为的格式:

[
[
{"id":9,"comment":"This is a normal comment to the post SON","commentername":"noforget","parentID":42,"commentVotes":0,"isWhat":"comment","voteToID":null,"votername":null,"voteAmount":null},
{"id":31,"comment":"reply btw","commentername":"noforget","parentID":9,"commentVotes":0,"isWhat":"reply","voteToID":null,"votername":null,"voteAmount":null},
{"id":32,"comment":"reply to reply btw","commentername":"noforget","parentID":31,"commentVotes":0,"isWhat":"reply","voteToID":null,"votername":null,"voteAmount":null}
],
[
{"id":22,"comment":"new made comment","commentername":"noforget","parentID":42,"commentVotes":0,"isWhat":"comment","voteToID":null,"votername":null,"voteAmount":null}
],
[
{"id":23,"comment":"just let me ","commentername":"noforget","parentID":42,"commentVotes":0,"isWhat":"comment","voteToID":null,"votername":null,"voteAmount":null}
]
]

换句话说,我希望将主要评论及其所有回复(以及这些回复的回复等)放在一个数组中,该数组位于一个数组中

关于数据的几点说明,如果是评论,"isWhat" 可以是 "comment""reply" ,这意味着它只是对帖子的普通评论而不是回复,并且它的parentID是帖子的ID(你不必担心这个), "isWhat: reply"parentID 是它回复的评论的 parentID

感谢您的宝贵时间!

最佳答案

这里你真的不需要递归;如果评论的顺序是 parent 总是在 child 之前(这是非常安全的假设),这可以在一次通过中完成。

假设 comments 是您输入数组的名称:

var result = [];
var threadsById = {};

for (let comment of comments) {
if (comment.isWhat == 'comment') {
var thread = [];
result.push(thread);
threadsById[comment.id] = thread;
}
else {
threadsById[comment.parentID].push(comment);
}
}

关于javascript - 根据条件将简单数据对象转换为对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49997826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com