【问题标题】:Comparing two JSONs in NodeJs & Express比较 NodeJs 和 Express 中的两个 JSON
【发布时间】:2020-01-17 21:17:02
【问题描述】:

我正在尝试比较 NodeJs 中的两个 JSONs 文件。 如果有与地点 ID 匹配的评论,我需要将评论数据推送到地点 JSON 中。如果没有匹配的评论,它会推送一个空数组。

代码如下:

//放置 JSON

[{
        "id": 1,
        "title": "Hotel in Sidney",
        "maxNumberOfGuests": 5,
        "description": "Quiet place by the water.",
        "createdAt": "2019/12/7 14:34",
        "price": 120
    },
    {
        "id": 2,
        "title": "Cabin in Italy",
        "maxNumberOfGuests": 2,
        "description": "Romantic lake cabin for two.",
        "createdAt": "2019/4/7 10:00",
        "price": 250
    }
]

//审查 JSON

[{
        "id": 1,
        "numberOfStars": 3,
        "content": "Comfy place",
        "placeId": 1,
        "createdAt": 12345
    },
    {
        "id": 2,
        "numberOfStars": 4,
        "content": "Awesome lake view.",
        "placeId": "",
        "createdAt": 23456
    }
]

这是想要的结果:

[{
        "id": 1,
        "title": "Hotel in Sidney",
        "maxNumberOfGuests": 5,
        "description": "Quiet place by the water.",
        "createdAt": "2019/12/7 14:34",
        "reviews": [{
            "id": 1,
            "numberOfStars": 3,
            "content": "Comfy place",
            "placeId": 1,
            "createdAt": 12345
        }],
        "price": 120
    },
    {
        "id": 2,
        "title": "Cabin in Italy",
        "maxNumberOfGuests": 2,
        "description": "Romantic lake cabin for two.",
        "createdAt": "2019/4/7 10:00",
        "reviews": [],
        "price": 250
    }
]

这是我能走多远:

places.forEach(p => {
  const { id } = p;
  console.log(id);
  return id;
});

reviews.forEach(r => {
  const { id, numberOfStars, content, placeId, createdAt } = r;
  // console.log(id, numberOfStars, content, placeId, createdAt);
  console.log(r);
  return r;
});

//node express routes to places where will display the desired result. 

router.get('/places', function(req, res) {
  res.json(places);
});

我无法让它工作,需要一些帮助。

提前致谢。

【问题讨论】:

    标签: javascript node.js json express


    【解决方案1】:

    试试这个

    let places =[{
            "id": 1,
            "title": "Hotel in Sidney",
            "maxNumberOfGuests": 5,
            "description": "Quiet place by the water.",
            "createdAt": "2019/12/7 14:34",
            "price": 120
        },
        {
            "id": 2,
            "title": "Cabin in Italy",
            "maxNumberOfGuests": 2,
            "description": "Romantic lake cabin for two.",
            "createdAt": "2019/4/7 10:00",
            "price": 250
        }
    ];
    
    let reviews =[{
            "id": 1,
            "numberOfStars": 3,
            "content": "Comfy place",
            "placeId": 1,
            "createdAt": 12345
        },
        {
            "id": 2,
            "numberOfStars": 4,
            "content": "Awesome lake view.",
            "placeId": "",
            "createdAt": 23456
        }
    ];
    places.forEach(function(place) {
      place.reviews = reviews.filter(review => review.placeId ===place.id);
    });
    
    
    console.log(places);

    【讨论】:

      【解决方案2】:

      我首先将reviews 数组缩减为一个以placeId 为键的对象。

      const reviews = [{
          "id": 1,
          "numberOfStars": 3,
          "content": "Comfy place",
          "placeId": 1,
          "createdAt": 12345
        },
        {
          "id": 2,
          "numberOfStars": 4,
          "content": "Awesome lake view.",
          "placeId": "",
          "createdAt": 23456
        }
      ];
      
      const reviewsHashmap = reviews.reduce((acc, review) => {
        if (!review.placeId) return acc;
        acc[review.placeId] = acc[review.placeId] || [];
        acc[review.placeId].push(review);
        return acc;
      }, {})
      

      这使得添加评论属性时性能更高,因为现在您不需要为每个地方一次又一次地过滤评论数组。

      const places = [
        {
          "id": 1,
          "title": "Hotel in Sidney",
          "maxNumberOfGuests": 5,
          "description": "Quiet place by the water.",
          "createdAt": "2019/12/7 14:34",
          "price": 120
        },
        {
          "id": 2,
          "title": "Cabin in Italy",
          "maxNumberOfGuests": 2,
          "description": "Romantic lake cabin for two.",
          "createdAt": "2019/4/7 10:00",
          "price": 250
        }
      ];
      
      const placesWithReviews = places.map(place => ({
        ...place,
        reviews: reviewsHashmap[place.id] || []
      }))
      

      现在您应该拥有原始的 places 数组,但每个位置都有一个额外的 reviews 属性。

      【讨论】:

        猜你喜欢
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 2020-06-11
        • 2016-09-13
        • 1970-01-01
        • 2012-12-26
        • 2020-07-22
        • 2014-03-02
        相关资源
        最近更新 更多