0

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()
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
Derry Uk
  • 91
  • 2
  • 9

1 Answers1

3

You can iterate over one of your arrays using .map(), where for each iteration, you can grab the associated value from the other array using the current index (this is passed as the second parameter to your .map() callback). For each call of your callback, you can return a new object of your desired obj shape, with each property set like so:

const likes = 176; 
const totalcommenters = [ 'john doe1', 'john doe 2', 'john doe 3', 'john doe 4', 'john doe 5', ];
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', ];

const res = totalcommenters.map((commenter, i) => ({
  commenter,
  comment: totalComment[i],
  numberOfLikes: likes
}));
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • Note to OP that when using arrow function syntax it's imperative you use parentheses around the object reference when returning an object reference in the callback function invocation. Without it JS treats it as a block. One of the quirks of using arrow function syntax. Nice solution though! – AaronS Mar 18 '21 at 08:39
  • @AaronS Thanks, and good point, more info can be found here if OP wants it: [ECMAScript 6 arrow function that returns an object](https://stackoverflow.com/q/28770415) – Nick Parsons Mar 18 '21 at 08:58
  • 1
    Sure, will do that – Derry Uk Mar 18 '21 at 10:00