簡體   English   中英

JavaScript。 如果總和 == 0,則在數組中查找某個參數的總和以生成彈出窗口

[英]JavaScript. Find the sum of a certain parameter in an array to produce a popup if that sum == 0

$(function () {
    $('.performance-container').each(function () {
        var container = $(this);
        var updateForm = $('.performance-form', container);
        var chartCanvas = $('.performance-canvas', container)[0];
        var chart = null;
        $('.performance-datetimepicker', container).datetimepicker({
            widgetPositioning: {
                horizontal: "auto",
                vertical: "bottom"
            }
        });
        updateForm.submit(function (event) {
            window.classiDocs.showLoadingOverlay(container.parents('.x_panel'));
            $.post(window.classiDocs.reporting.routes.classificationPerformance, updateForm.serialize(),
                function (data) {
                    var chartData = data.Items.map(function (historyItem) {
                        return { x: historyItem.Timestamp, y: historyItem.Value };                    
                    });
                    if (!chart) {
                        initialize(chartData);
                    } else {
                        chart.data.datasets[0].data = chartData;
                        chart.update();
                    }
                })
                .always(function () {
                    window.classiDocs.hideLoadingOverlay(container.parents('.x_panel'));
                });
            event.preventDefault();
        });
        

        $(updateForm).submit();
        function initialize(data) {
            console.log(data);
            chart = new Chart(chartCanvas.getContext("2d"), {
                type: "line",
                data: {
                    datasets: [
                        {
                            backgroundColor: "rgba(38, 185, 154, 0.31)",
                            borderColor: "rgba(38, 185, 154, 0.7)",
                            pointBorderColor: "rgba(38, 185, 154, 0.7)",
                            pointBackgroundColor: "rgba(38, 185, 154, 0.7)",
                            pointHoverBackgroundColor: "#fff",
                            pointHoverBorderColor: "rgba(220,220,220,1)",
                            pointBorderWidth: 1,
                            lineTension: 0,
                            tension: 0,
                            data: data
                        }
                    ]
                },
                options: {
                    legend: {
                        display: false
                    },
                    tooltips: {
                        callbacks: {
                            title: function (items) {
                                return new Date(items[0].xLabel).toLocaleString();
                            },
                            label: function (item) {
                                return item.y.toLocaleString();
                            },
                            footer: function () {
                                return null;
                            }
                        }
                    },
                    scales: {
                        xAxes: [
                            {
                                type: "time",
                                scaleLabel: {
                                    display: true,
                                    labelString: 'Time Span',
                                    fontSize: 20,
                                    fontColor: "#555555"
                                }
                            }
                        ],
                        yAxes: [
                            {
                                scaleLabel: {
                                    display: true,
                                    labelString: 'Number Of Classifications',
                                    fontSize: 20,
                                    fontColor: "#555555"
                                },
                                ticks: {
                                    beginAtZero: true
                                }
                            }
                        ]
                    }
                }
            });
        }
    });
});

大家好。 我對開發還很陌生,所以請原諒對術語的任何不正確使用。 我的目標是在 y 軸上的值之和等於 0 時添加一個彈出窗口。 在此代碼中返回兩個對象數組。 這些數組中的數據在控制台中返回如下:

0: {x: "2020-10-01T00:00:00", y: 0}
1: {x: "2020-10-01T00:30:00", y: 0}
2: {x: "2020-10-01T01:00:00", y: 0}
3: {x: "2020-10-01T01:30:00", y: 0}
4: {x: "2020-10-01T02:00:00", y: 0}
5: {x: "2020-10-01T02:30:00", y: 0}

此圖表正在動態使用,因此此腳本生成兩個圖表。 我如何遍歷兩個數組並找到每個數組的“y”之和? 提前致謝。

你可以使用Array.prototype.reduce ,這里是完整的代碼

if (data.reduce((a, v) => a + v.y, 0) === 0) {
  alert('The sum of data.y is zero!');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM