简体   繁体   中英

How to update the values in an Object with results from an array

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()

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM