英文:
How to compare two values with other three values аnd to visualize all the rows that meet the condition using vanilla js
问题
我需要创建一个洪水预警系统,并将两个值 level_11
和 level_fw
与这个 api 中的三个值 ALERT_yewoll
、ALERT_orange
和 ALERT_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_fw
和 level_11
的值在 ALERT_yewoll
、ALERT_orange
和 ALERT_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 = 'http://194.141.118.43:3010/alldata';
// 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 = "YYYY-MM-DD HH:mm:ss";
if (AllData.level_11 >= 222.50 /*MikeAllData.ALERT_yewoll*/ && AllData.level_11 < 222.50 /*MikeAllData.ALERT_orange*/) {
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 /*MikeAllData.ALERT_yewoll*/ && AllData.level_fw < 222.50 /*MikeAllData.ALERT_orange*/) {
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();
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 = [
{"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 "No warning"
else if (value < ORANGE)
return "Yellow warning"
else if (value < RED)
return "Orange warning"
else return "Red warning"
}
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 = "Row " + 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 = "Level " + 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
}
<!-- language: lang-html -->
<html>
<body>
<button type="button" id="buttonOne">Data set one</button>
<button type="button" id="buttonTwo">Data set two</button>
<div id="warningBox"></div>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论