I have a data structure problem that I have been trying solve in nodejs but it is giving a difficult time and I will appreciate any help.
I have an object:
let obj = {
commenter: '',
comment: '',
numberOflikes: 0,
}
And a container:
let container = []
I have an array of comments and commenter, and a value for number of likes:
//total likes
let likes = 176;
// total commenters
const totalcommenters = [
'john doe1',
'john doe 2',
'john doe 3',
'john doe 4',
'john doe 5',
];
// total comments
const totalComment = [
'this is a comment one',
'this is a comment two',
'this is a comment three',
'this is a comment four',
'this is a comment five',
];
I want to update the objects, and place them into the container. This is my desired result:
[
{
commenter: 'john doe1',
comment: 'this is a comment one',
numberOflikes: 176,
},
{
commenter: 'john doe2',
comment: 'this is a comment two',
numberOflikes: 176,
},
{
commenter: 'john doe3',
comment: 'this is a comment three',
numberOflikes: 176,
},
{
commenter: 'john doe4',
comment: 'this is a comment four',
numberOflikes: 176,
},
{
commenter: 'john doe5',
comment: 'this is a comment five',
numberOflikes: 176,
}
]
But i am not getting the desired results. This is what I have done so far, I tried to approach it in an OOP fashion:
class Data {
// loop though and update comments
static updateComment() {
let mapFunc = (comment) => {
return { ...obj, comment: comment }
}
let mapped = totalComment.map(mapFunc)
}
static updateCommenters() {
let mapFunc = (commenter) => {
return { ...obj, commenter: commenter }
}
let mappedcommenters = totalcommenters.map(mapFunc)
}
}
Data.updateComment()
Data.updateCommenters()