有人可以帮我理解这个循环在做什么吗?我对 Node JS 很陌生,我只想用 grafana、influxdb 和 Node js 过滤显示 csv 数据。 (没有 nom 模块)
let orders = []
for (let line of lines) {
let columns = line.split(";");
let orderID = columns[1]
let type = columns[2]
if (!orders[orderID]) {
orders[orderID] = {
id: orderID
events: []
};
}
orders[orderID].events.push(type);
}
我尤其不明白 if 循环。它有什么作用?
最佳答案
for 循环正在获取行数组。列被“;”分隔终结者。
line //=========> contains text 'text A ; text B'
let columns = line.split(";");
列被“;”分隔终止符,因此 columns 变量将包含带分区的数组。
console.log(columns) //=======> ['text A','text B']
接下来,orderID
变量将简单地存储接收到的任何 id,与类型类似
let orderID = columns[1]
let type = columns[2]
接下来,if 条件检查订单数组是否不包含任何具有 OrderId
索引的元素。如果只是意味着它检查订单是否存在。
如果不存在,则条件被视为 true,并且订单数组将插入 OrderId
对象和空事件数组。
if (!orders[orderID]) {
orders[orderID] = {
id: orderID
events: []
};
}
最后一行只是将从当前事件数组中的对象接收到的类型键推送到当前事件数组中。
orders[orderID].events.push(type);
let orders = []
for (let line of lines) {
let columns = line.split(";");
let orderID = columns[1]
let type = columns[2]
if (!orders[orderID]) {
orders[orderID] = {
id: orderID
events: []
};
}
orders[orderID].events.push(type);
希望有帮助:)
关于javascript - 这个循环是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60919607/