【问题标题】:How to show image based on an object property in JavaScript如何在 JavaScript 中基于对象属性显示图像
【发布时间】:2022-02-03 01:50:57
【问题描述】:

我有一个带有四个键的变量。当 icon 键的值为 01d 时,我需要显示引用该值的图标,该值位于 img 文件夹中。我怎样才能使这个条件?

我有以下变量解构,下面我把它的返回。

let [dt1, dt2, dt3, dt4, dt5, dt6, dt7, dt8] = urlJson 返回:

{ dt: 1643803200, day: 12.84, description: 'clear sky', icon: '01d' }
{ dt: 1643889600, day: 14.56, description: 'overcast clouds', icon: '04d' }
{ dt: 1643976000, day: 14.85, description: 'overcast clouds', icon: '04d' }
{ dt: 1644062400, day: 14.41, description: 'broken clouds', icon: '04d' }
{ dt: 1644148800, day: 14.99, description: 'clear sky', icon: '01d' }
{ dt: 1644235200, day: 15.68, description: 'few clouds', icon: '02d' }
{ dt: 1644321600, day: 14.95, description: 'broken clouds', icon: '04d' }
{ dt: 1644408000, day: 16.37, description: 'overcast clouds', icon: '04d' }

代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link id="mystylesheet" type="text/css" rel="stylesheet" href="style.css">

  <title>Document</title>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>

<body>
  <div id="wrapper">
    <h1 class="city"></h1>
    <h2><span id="tags">0</span>ºC</h2>
  </div>
  <script>
    const urlJsonString = $.getJSON('https://api.openweathermap.org/data/2.5/onecall?lat=38.7267&lon=-9.1403&exclude=current,hourly,minutely,alerts&units=metric&appid={APPID}', function (data) {
      let urlJson = data.daily.map(({ dt, temp: { day }, weather: [{ description, icon }] }) => ({ dt, day, description, icon }))
      let [dt1, dt2, dt3, dt4, dt5, dt6, dt7, dt8] = urlJson
      var date = dt1.dt

      let result = `Temperature: ${dt1.day} ºC<br>
      Dia: ${date}<br>
      ${dt1.icon}`
      $(".city").html(result);
    });
  </script>
</body>

</html>

【问题讨论】:

  • urlJsonStringurlJson 是这些变量的可怕名称,因为它们不包含与其当前名称相关的任何内容。
  • 标题和之间的联系在哪里?“我需要显示引用这个值的图标......我怎样才能使它有条件?”
  • 也好像urlJson[dt1,dt2....date和'result'都可以标记const
  • @Andreas 更不用说他在代码中列出了 8 个日期,但标题说 7
  • 你基本上只是想用&lt;image src='...' /&gt;替换${dt1.icon}

标签: javascript html image if-statement conditional-statements


【解决方案1】:

看看这是否适合你。我没有访问此 API 的密钥,所以我使用了您的虚构数据。

请确保您的“图标”是您的图片网址

https://jsfiddle.net/ep9hsxLf/

let urlJson = [
  { dt: 1643803200, day: 12.84, description: 'clear sky', icon: '01d' },
  { dt: 1643889600, day: 14.56, description: 'overcast clouds', icon: '04d' },
  { dt: 1643976000, day: 14.85, description: 'overcast clouds', icon: '04d' },
  { dt: 1644062400, day: 14.41, description: 'broken clouds', icon: '04d' },
  { dt: 1644148800, day: 14.99, description: 'clear sky', icon: '01d' },
  { dt: 1644235200, day: 15.68, description: 'few clouds', icon: '02d' },
  { dt: 1644321600, day: 14.95, description: 'broken clouds', icon: '04d' },
  { dt: 1644408000, day: 16.37, description: 'overcast clouds', icon: '04d' }
]

var htmlResult = '';

urlJson.forEach(item => {

  var date = new Date(item.dt)

  htmlResult += `Temperature: ${item.day} ºC<br>
  Dia: ${date}<br>
  ${item.icon}<br><br>`
});


$(".city").html(htmlResult);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<div class="city"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2014-06-14
    相关资源
    最近更新 更多