You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
567 B
22 lines
567 B
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); |
|
};
|
|
|