如何根据匹配的名称值在车把中呈现两组数据

How to render two sets of data in handlebars on matching name value

我在 handlebars 和 express 中呈现网页。数据由具有两个对象的 JSON 文档驱动,一个是一组日期,另一个包含详细信息。每个日期动态创建一个包含所需数据的网页。下面是我正在处理的数据的一个简单示例。

JSON

{
    "calendar": {
        "jan01": {
            "date": "January 1st",
            "weather": "cloudy"

        },
        "jan02": {
            "date": "January 2nd",
            "weather": "sunny"
        },
        ...
        "dec31": {
            "date": "December 31st",
            "weather": "cloudy"
        }
    },
    "weatherDetails": {
        "sunny": {
            "img": "https://sunny image"
        },
        "cloudy": {
            "img": "http://cloudy image"
        }
    }
}

我通过以下方式从 express 传递数据:

routes.get('/day/:date', (req, res) => {
  const urlDate = req.params.date
  res.render('day', {
    day: calendar[urlDate].date,
    weather: calendar[urlDate].weather
})

我可以提取实际的词(晴天或多云),但我还想在 weatherDetails 对象中获得相应的图像 link。

我不知道从哪里开始考虑解决方案。无论是通过合并匹配名称上的数据来创建一个新对象,还是在 handlebars 中有一个辅助函数可以做到这一点。

我假设此处的日历是 JSON 对象中日历键的对象,因此您有另一个名为 weatherDetails 的变量,它具有 JSON 对象中 weatherDetails 键的对象。

为什么不直接解析图像并将其作为要传递给渲染函数的对象中的另一个键传递

routes.get('/day/:date', (req, res) => {
  const urlDate = req.params.date
  res.render('day', {
    day: calendar[urlDate].date,
    weather: calendar[urlDate].weather,
    weatherImage: weatherDetails[calendar[urlDate].weather].img 
})

这是假设 JSON 文件中的 weatherDetails 对象考虑了所有天气状况。