export const connectToParent = (list) => { let comments = []; for (let item of list) { comments.push({ ...item, child: [] }); } for (let comment1 of comments) { if (comment1.pid !== null) { const index = comments.indexOf( comments.filter((comment2) => comment2.id === comment1.pid)[0] ); comments[index] = { ...comments[index], child: [...comments[index].child, comment1], }; } else { } } // comments = comments.filter((comment) => comment.pid === null); console.log(comments); };