简体   繁体   English

如何使用数组的结果更新 Object 中的值

[英]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.我有一个数据结构问题,我一直在尝试在 nodejs 中解决,但这给了我一个困难的时期,我将不胜感激。

I have an object:我有一个 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:这就是我到目前为止所做的,我试图以 OOP 方式接近它:

    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).您可以使用.map()迭代您的 arrays 之一,对于每次迭代,您可以使用当前索引从另一个数组中获取关联值(这作为第二个参数传递给您的.map()回调)。 For each call of your callback, you can return a new object of your desired obj shape, with each property set like so:对于回调的每次调用,您都可以返回所需obj形状的新 object,每个属性设置如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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