How to compare two values with other three values аnd to visualize all the rows that meet the condition using vanilla js

huangapple go评论59阅读模式
英文:

How to compare two values with other three values аnd to visualize all the rows that meet the condition using vanilla js

问题

我需要创建一个洪水预警系统,并将两个值 level_11level_fw 与这个 api 中的三个值 ALERT_yewollALERT_orangeALERT_red 进行比较。

目前,我是这样进行检查的,但是当我用手动值(例如 225.50,这是行中的 [0] 索引)替换 MikeAllData.ALERT_yewoll 时,我只看到一行,但我需要看到所有满足条件的行。

const urlCheckForWarnings = 'http://194.141.118.43:3010/alldata';

// 创建异步函数以获取数据
async function getDataWarningsMikeFW() {
    try {
        // 使用 fetch 获取响应
        const responseAllData = await fetch(urlCheckForWarnings);
        const allDataModels = await responseAllData.json();
        // 触发 listValues 函数并传递结果
        listValues(allDataModels);
        console.log(allDataModels);
    } catch (error) {
        console.log(error);
    }
}

function listValues(allDataModels) {
    // 循环遍历每个结果并追加数据。
    allDataModels.floodguard_alldata.rows.map(function (AllData, i) {
        const formatDate = "YYYY-MM-DD HH:mm:ss";

        if (AllData.level_11 >= 222.50 && AllData.level_11 < 222.50) {
            document.getElementById("mikeFWwarnings").innerHTML = "Предупреждение 1: Жълт код за: " + AllData.substring + " за дата: " + moment(AllData.date).format(formatDate);
        }

        if (AllData.level_11 >= AllData.ALERT_orange && AllData.level_11 < AllData.ALERT_red) {
            document.getElementById("mikeFWwarnings").innerHTML = "Предупреждение 2: Оранжев код за: " + AllData.substring + " за дата: " + moment(AllData.date).format(formatDate);
        }

        if (AllData.level_11 >= AllData.ALERT_red) {
            document.getElementById("mikeFWwarnings").innerHTML = "Предупреждение 3: Червен код за: " + AllData.substring + " за дата: " + moment(AllData.date).format(formatDate);
        }

        if (AllData.level_fw >= 222.50 && AllData.level_fw < 222.50) {
            document.getElementById("mikeFWwarnings").innerHTML = "Предупреждение 1: Жълт код за: " + AllData.substring + " за дата: " + moment(AllData.date).format(formatDate);;
        }

        if (AllData.level_fw >= AllData.ALERT_orange && AllData.level_fw < AllData.ALERT_red) {
            document.getElementById("mikeFWwarnings").innerHTML = "Предупреждение 2: Оранжев код за: " + AllData.substring + " за дата: " + moment(AllData.date).format(formatDate);;
        }

        if (AllData.level_fw >= AllData.ALERT_red) {
            document.getElementById("mikeFWwarnings").innerHTML = "Предупреждение 3: Червен код за: " + AllData.substring + " за дата: " + moment(AllData.date).format(formatDate);;
        }
    });
};

getDataWarningsMikeFW();

我需要执行的条件是,如果 level_fwlevel_11 的值在 ALERT_yewollALERT_orangeALERT_red 的范围内,则设置 Code 1、Code 2 或 Code 3 警告消息。

我不确定我的检查是否正确,以及如何可视化所有满足条件的索引,而不仅仅是第一个索引?

英文:

I need to make a flood warning system and compare two values level_11 and level_fw with these three values ALERT_yewoll ALERT_orange and ALERT_red from this api.

For now I do the check this way, but when I replace MikeAllData.ALERT_yewoll with manual value for example 225.50 (this is for [0] index in the rows) I see only one row, but I need to see all rows which meet the condition.

const urlCheckForWarnings = &#39;http://194.141.118.43:3010/alldata&#39;;
// Create Asynchronous function to grab the data 
async function getDataWarningsMikeFW() {
try {
// We are using fetch to get the response
const responseAllData = await fetch(urlCheckForWarnings);
const allDataModels = await responseAllData.json();
// Trigger the listData function and pass the result
listValues(allDataModels);
console.log(allDataModels);
} catch (error) {
console.log(error);
}
}
function listValues(allDataModels) {
// Loop through each result and append the data.
allDataModels.floodguard_alldata.rows.map(function (AllData, i) {
const formatDate = &quot;YYYY-MM-DD HH:mm:ss&quot;;
if (AllData.level_11 &gt;= 222.50 /*MikeAllData.ALERT_yewoll*/ &amp;&amp; AllData.level_11 &lt; 222.50 /*MikeAllData.ALERT_orange*/) {
document.getElementById(&quot;mikeFWwarnings&quot;).innerHTML = &quot;Предупреждение 1: Жълт код за: &quot; + AllData.substring + &quot; за дата: &quot; + moment(AllData.date).format(formatDate);
}
if (AllData.level_11 &gt;= AllData.ALERT_orange &amp;&amp; AllData.level_11 &lt; AllData.ALERT_red) {
document.getElementById(&quot;mikeFWwarnings&quot;).innerHTML = &quot;Предупреждение 2: Оранжев код за: &quot; + AllData.substring + &quot; за дата: &quot; + moment(AllData.date).format(formatDate);
}
if (AllData.level_11 &gt;= AllData.ALERT_red) {
document.getElementById(&quot;mikeFWwarnings&quot;).innerHTML = &quot;Предупреждение 3: Червен код за: &quot; + AllData.substring + &quot; за дата: &quot; + moment(AllData.date).format(formatDate);
}
if (AllData.level_fw &gt;= 222.50 /*MikeAllData.ALERT_yewoll*/ &amp;&amp; AllData.level_fw &lt; 222.50 /*MikeAllData.ALERT_orange*/) {
document.getElementById(&quot;mikeFWwarnings&quot;).innerHTML = &quot;Предупреждение 1: Жълт код за: &quot; + AllData.substring + &quot; за дата: &quot; + moment(AllData.date).format(formatDate);;
}
if (AllData.level_fw &gt;= AllData.ALERT_orange &amp;&amp; AllData.level_fw &lt; AllData.ALERT_red) {
document.getElementById(&quot;mikeFWwarnings&quot;).innerHTML = &quot;Предупреждение 2: Оранжев код за: &quot; + AllData.substring + &quot; за дата: &quot; + moment(AllData.date).format(formatDate);;
}
if (AllData.level_fw &gt;= AllData.ALERT_red) {
document.getElementById(&quot;mikeFWwarnings&quot;).innerHTML = &quot;Предупреждение 3: Червен код за: &quot; + AllData.substring + &quot; за дата: &quot; + moment(AllData.date).format(formatDate);;
}
});
};
getDataWarningsMikeFW();

The condition I have to do is if values of level_fw and level_11 are in the range between ALERT_yewoll ALERT_orange and ALERT_red To set Code 1, Code 2 or Code 3 warning messages.

I'm not sure if my check is correct and how can I visualize all indexes that meet the condition and not just the first index?

答案1

得分: 1

以下是代码的翻译部分:

// CONSTANTS
let YELLOW = 500;
let ORANGE = 750;
let RED = 1000;

// DATA
let dataSetOne = [
    {"level_11": 600, "level_fw": 800},
    {"level_11": 800, "level_fw": 400},
    {"level_11": 400, "level_fw": 1200}
];
let dataSetTwo = [
    {"level_11": 200, "level_fw": 400},
    {"level_11": 1200, "level_fw": 700},
    {"level_11": 300, "level_fw": 800},
    {"level_11": 1200, "level_fw": 1200},
    {"level_11": 800, "level_fw": 600},
]

// ELEMENTS
let warningBox = document.getElementById("warningBox");

// BUTTONS
document.getElementById("buttonOne").addEventListener("click", () => listValues(dataSetOne))
document.getElementById("buttonTwo").addEventListener("click", () => listValues(dataSetTwo))

// FUNCTIONS
function listValues(allDataModels) {
    getRightNumberOfWarningElements(allDataModels);
    allDataModels.forEach((data, index) =>
        fillInRowWarnings(data, index + 1)
    )
}

function fillInRowWarnings(data, row) {
    let element_11 = document.getElementById(warningRowChildId(row, "11"))
    let element_fw = document.getElementById(warningRowChildId(row, "fw"))
    element_11.innerText = getWarningText(data.level_11);
    element_fw.innerText = getWarningText(data.level_fw);
}

function getWarningText(value) {
    if (value < YELLOW)
        return "无警告";
    else if (value < ORANGE)
        return "黄色警告";
    else if (value < RED)
        return "橙色警告";
    else return "红色警告";
}

function getRightNumberOfWarningElements(allDataModels) {
    if (allDataModels.length > warningBox.childElementCount)
        addWarningElements(warningBox.childElementCount, allDataModels.length);
    if (allDataModels.length < warningBox.childElementCount)
        removeWarningElements(warningBox.childElementCount, allDataModels.length);
}

function addWarningElements(from, to) {
    for (i = from + 1; i <= to; i++)
        warningBox.appendChild(createWarningRow(i))
}

function removeWarningElements(from, to) {
    for (i = from; i >= to + 1; i--)
        warningBox.removeChild(warningBox.lastChild)
}

function createWarningRow(number) {
    let row = document.createElement("div");
    row.setAttribute("id", "warningRow" + number);
    let rowTitle = document.createElement("h4");
    rowTitle.innerText = "行 " + number;
    row.appendChild(rowTitle);
    addSubTitle(row, "11");
    addNewRowChild(row, number, "11");
    addSubTitle(row, "fw");
    addNewRowChild(row, number, "fw");
    return row;
}

function addSubTitle(row, subTitleText) {
    let subTitle = document.createElement("h5");
    subTitle.innerText = "级别 " + subTitleText;
    row.appendChild(subTitle);
}

function addNewRowChild(row, rowNumber, levelType) {
    let newChild = document.createElement("p");
    newChild.setAttribute("id", warningRowChildId(rowNumber, levelType));
    row.appendChild(newChild);
}

function warningRowChildId(rowNumber, levelType) {
    return "warningRow_" + rowNumber + "_level_" + levelType;
}
<html>
<body>
<button type="button" id="buttonOne">数据集一</button>
<button type="button" id="buttonTwo">数据集二</button>
<div id="warningBox"></div>
</body>
</html>

希望这有助于理解代码的功能。

英文:

Ok, here's a pretty rough and ready example of how you can add and remove elements on your page and populate the correct elements with the right warnings.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// CONSTANTS
let YELLOW = 500;
let ORANGE = 750;
let RED = 1000;
// DATA
let dataSetOne = [
{&quot;level_11&quot;: 600, &quot;level_fw&quot;: 800},
{&quot;level_11&quot;: 800, &quot;level_fw&quot;: 400},
{&quot;level_11&quot;: 400, &quot;level_fw&quot;: 1200}
];
let dataSetTwo = [
{&quot;level_11&quot;: 200, &quot;level_fw&quot;: 400},
{&quot;level_11&quot;: 1200, &quot;level_fw&quot;: 700},
{&quot;level_11&quot;: 300, &quot;level_fw&quot;: 800},
{&quot;level_11&quot;: 1200, &quot;level_fw&quot;: 1200},
{&quot;level_11&quot;: 800, &quot;level_fw&quot;: 600},
]
// ELEMENTS
let warningBox = document.getElementById(&quot;warningBox&quot;);
// BUTTONS
document.getElementById(&quot;buttonOne&quot;).addEventListener(&quot;click&quot;, () =&gt; listValues(dataSetOne))
document.getElementById(&quot;buttonTwo&quot;).addEventListener(&quot;click&quot;, () =&gt; listValues(dataSetTwo))
// FUNCTIONS
function listValues(allDataModels) {
getRightNumberOfWarningElements(allDataModels);
allDataModels.forEach ((data, index) =&gt;
fillInRowWarnings(data, index + 1)
)
}
function fillInRowWarnings(data, row) {
let element_11 = document.getElementById(warningRowChildId(row, &quot;11&quot;))
let element_fw = document.getElementById(warningRowChildId(row, &quot;fw&quot;))
element_11.innerText = getWarningText(data.level_11);
element_fw.innerText = getWarningText(data.level_fw);
}
function getWarningText(value) {
if (value &lt; YELLOW)
return &quot;No warning&quot;
else if (value &lt; ORANGE)
return &quot;Yellow warning&quot;
else if (value &lt; RED)
return &quot;Orange warning&quot;
else return &quot;Red warning&quot;
}
function getRightNumberOfWarningElements(allDataModels) {
if (allDataModels.length &gt; warningBox.childElementCount)
addWarningElements(warningBox.childElementCount, allDataModels.length);
if (allDataModels.length &lt; warningBox.childElementCount)
removeWarningElements(warningBox.childElementCount, allDataModels.length);
}
function addWarningElements(from, to) {
for (i = from + 1; i&lt;= to; i++)
warningBox.appendChild(createWarningRow(i))
}
function removeWarningElements(from, to) {
for (i = from; i&gt;= to + 1; i--)
warningBox.removeChild(warningBox.lastChild)
}
function createWarningRow(number) {
let row = document.createElement(&quot;div&quot;)
row.setAttribute(&quot;id&quot;, &quot;warningRow&quot; + number);
let rowTitle = document.createElement(&quot;h4&quot;);
rowTitle.innerText = &quot;Row &quot; + number;
row.appendChild(rowTitle);
addSubTitle(row, &quot;11&quot;)
addNewRowChild(row, number, &quot;11&quot;);
addSubTitle(row, &quot;fw&quot;)
addNewRowChild(row, number, &quot;fw&quot;);
return row;
}
function addSubTitle(row, subTitleText) {
let subTitle = document.createElement(&quot;h5&quot;);
subTitle.innerText = &quot;Level &quot; + subTitleText;
row.appendChild(subTitle);
}
function addNewRowChild(row, rowNumber, levelType) {
let newChild = document.createElement(&quot;p&quot;)
newChild.setAttribute(&quot;id&quot;, warningRowChildId(rowNumber, levelType));
row.appendChild(newChild)
}
function warningRowChildId(rowNumber, levelType) {
return &quot;warningRow_&quot; + rowNumber + &quot;_level_&quot; + levelType
}

<!-- language: lang-html -->

&lt;html&gt;
&lt;body&gt;
&lt;button type=&quot;button&quot; id=&quot;buttonOne&quot;&gt;Data set one&lt;/button&gt;
&lt;button type=&quot;button&quot; id=&quot;buttonTwo&quot;&gt;Data set two&lt;/button&gt;
&lt;div id=&quot;warningBox&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 17:45:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定