javascript - 这个循环是什么?

标签 javascript node.js

有人可以帮我理解这个循环在做什么吗?我对 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/

相关文章:

java - npm安装java : error MSB4019: The imported project "D:\Microsoft.Cpp.Default.props" was not found

javascript - 如何防止滚动直到页面加载

javascript - 停止 .click() 监听器直到 fadeIn() 在 jQuery 中完成

javascript - 如何使 HTML 文本框不可点击

macos - 在 OSX Lion 上安装 NPM

javascript - 然后nodejs promise 只有一个参数而不是2个

javascript - 使用悬停事件发送垃圾邮件时元素可见性问题

javascript - JS 函数调用链中丢失上下文

node.js - 在git中 merge package.json(解决版本冲突)

node.js - 自动向 Express 部分 View 提供数据