javascript - 将 JSON 值组合成一个文本

标签 javascript json

我使用此 JavaScript 将 6 小时内的预报降雪值显示在 HTML 上。 HTML 上的文本显示...0.25、0.25、0.25、0.35、0.20、0.25,而不是 1.55。返回的 JSON 结果包含 6 个不同的预测降雪值,我需要将所有值添加到一个要显示的值中。

JSON 的响应看起来像这样...

{"success":true,"error":null,"response":[{"periods":[{"snowIN":0.25},{"snowIN":0.25},{"snowIN":0.25},{"snowIN":0.35},{"snowIN":0.20},{"snowIN":0.25}]}]}  

JS:

<script type="text/javascript" src="https://cdn.aerisapi.com/sdk/js/latest/aerisweather.min.js">
</script>
<script type="text/javascript">


    window.onload = () => {

        const target = document.getElementById('data-reading');
        const aeris = new AerisWeather('CLIENT_ID', 'CLIENT_SECRET');

        const request = aeris.api().endpoint('forecasts').place('pierre,sd').filter('1hr').limit('48');
        request.get().then((result) => {
            const data = result.data;
            const { periods } = data[0];
            if (periods) {
                periods.reverse().forEach(period => {
                    const snowIN = period.snowIN || '0.00';

                    const html = (`
                                 <p class="map-datavalue">${snowIN}"</p>
                            `);

                    target.insertAdjacentHTML('afterbegin', html);
                });
            }
        }); 
    }

</script>

最佳答案

您可以使用 Array.reduce 计算您的经期总数:

const target = document.getElementById('data-reading');

const result = {"success":true,"error":null,"response":[{"periods":[{"snowIN":0.25},{"snowIN":0.25},{"snowIN":0.25},{"snowIN":0.35},{"snowIN":0.20},{"snowIN":0.25}]}]} 

const data = result.response;
const { periods } = data[0];
if (periods) {
  const total = periods.reduce((sum, { snowIN }) => sum + snowIN, 0);
  const html = `<p class="map-datavalue">${total || '0.00'}</p>`;
  target.insertAdjacentHTML('afterbegin', html);
}
.map-datavalue { 
  font-family: "Arial",sans-serif;
  font-size: 20px;
  color: #FFFFFF;
  font-weight: 700;
  text-shadow: -2px 0 #000000, 0 2px #000000, 2px 0 #000000, 0 -2px #000000;
  z-index: 6;
}
<div class="map-datavalue" id="data-reading" style="position: absolute; left: 181px; top: 56px; width:76px; height:30px">&nbsp;</div>

只需将其插入到您的 if (periods) { ... } 中,如下所示:

request.get().then((result) => {
    const data = result.data;
    const { periods } = data[0];
    if (periods) {
        const total = periods.reduce((sum, { snowIN }) => sum + snowIN, 0);
        const html = `<p class="map-datavalue">${total || '0.00'}</p>`;
        target.insertAdjacentHTML('afterbegin', html);
    }
}); 

关于这一点:

const total = periods.reduce((sum, { snowIN }) => sum + snowIn, 0);

相当于:

let total = 0;
for (let i = 0; i < periods.length; ++i) {
    total += periods[i].snowIN;
}

但使用解构来获取 snowIN 属性,并使用 reduce 抽象出 for 循环。

关于javascript - 将 JSON 值组合成一个文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54726549/

相关文章:

javascript - 两点之间的直线

javascript - DataTables - 翻译与选择扩展相关的文本

javascript - 来自 Node JS 中查询 SELECT Mongoose 的 JSON 解析错误

java - GSON:如何在保持循环引用的同时防止 StackOverflowError?

json - Flutter HTTP获取JSON数据

javascript - 使用 JavaScript 可以读取/写入服务器上的文件

javascript - 通过原型(prototype)定义方法与在构造函数中使用它相比——真的有性能差异吗?

javascript - TinyMCE 编辑器图像管理器/在 Django 中上传

javascript - mongodb - 不明白为什么/如何使用 map-reduce

json - jq 按键递归搜索值,从