这个循环是什么



有人能帮我理解这个循环在做什么吗?我是NodeJS的新手,我只想用grafana、influxdb和NodeJS过滤显示csv数据。(无标称模块(

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(";"); 

列由";"拆分terminator,因此columns变量将包含带分区的数组。

console.log(columns) //=======> ['text A','text B']

接下来,orderID变量将简单地存储接收到的任何id,类似于的类型

let orderID = columns[1]
let type = columns[2]

接下来,if条件检查order数组是否不包含任何具有OrderId索引的元素。If只是指它检查订单是否存在。

如果不是,则条件被视为true,并插入带有对象OrderId和事件空数组的orders数组。

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);

希望有帮助:(

您有一个行(字符串(数组。

您使用for..of迭代此数组,它几乎等于简单的for,但您不直接使用索引项。

for (let i = 0; i < lines.length; i++) {
const line = lines[i];
console.log(line);
}
/* ~ equal to */
for (line of lines) {
console.log(line);
}

您用;分割每个line项目。这意味着您可以通过分隔符;将字符串line拆分为令牌数组。

const array = "A;B;C".split(';'); // ["A", "B", "C"]

然后获取令牌数组的第二个和第三个元素,并将它们分配给变量orderIdtype

const orderID = array[1]; // "B" (second)
const type = array[2]; // "C" (third)

然后检查orders数组中是否存在索引为orderID的元素。如果没有,你创建它。


let orders = [] 
/* ... */
if (!orders[orderID]) {
orders[orderID] = {
id: orderID,
events: []
}
}

然后输入type变量的events数组值。

orders[orderID].events.push(type);

我希望它能帮助你:(

让我们从解析CSV快捷方式开始:C命令S分离V值,但许多CSV文件使用分号而不是逗号。

for (let line of lines) { // iterate line by line from CSV file
let columns = line.split(";"); // split line by known separator (;)
// You must know what value is in which column
let orderID = columns[1] // get value from first column and bind it to 'orderID' variable
let type = columns[2] // get value from second column and bind it to 'type' variable 
// check did order on given position exists 
if (!orders[orderID]) {
// if not create empty one
orders[orderID] = {
id: orderID
events: []
};
}
// add `type` to order `events` 
orders[orderID].events.push(type);
}

//一些伪csv和行

const csv = `
test;0;usa
test;1;spain
test;1;japan
test;2;china
test;3;korea
test;3;singapore
`.trim();
const lines = csv.split("n");

let orders = [];
for (let line of lines) { // iterate line by lines
let [_, orderID, type] = line.split(";"); // split line by known separator (;), 2nd colum as orderID, 3rd as type
if (!orders[orderID]) { // check did order on given position exists or not, if not create empty one
orders[orderID] = {
id: orderID,
events: []
};
}
// add/pus `type` to order `events` 
orders[orderID].events.push(type);
}
console.log(orders);

// Output
/*
[ { id: '0', events: [ 'usa' ] },
{ id: '1', events: [ 'spain', 'japan' ] },
{ id: '2', events: [ 'china' ] },
{ id: '3', events: [ 'korea', 'singapore' ] } ]
*/

//使用reduce 相同

const orders2 = lines.reduce((all, item) => {
let [_, orderID, type] = item.split(";");
if (!all[orderID]) {
all[orderID] = {
id: orderID,
events: []
};
}
all[orderID].events.push(type);
return all;
}, []);
console.log(orders2);

正在运行:

// Some dummy csv and lines
const csv = `
test;0;usa
test;1;spain
test;1;japan
test;2;china
test;3;korea
test;3;singapore
`.trim();
const lines = csv.split("n");
let orders = [];
for (let line of lines) { // iterate line by lines
let [_, orderID, type] = line.split(";"); // split line by known separator (;), 2nd colum as orderID, 3rd as type
if (!orders[orderID]) { // check did order on given position exists or not, if not create empty one
orders[orderID] = {
id: orderID,
events: []
};
}
// add/pus `type` to order `events` 
orders[orderID].events.push(type);
}
console.log(orders);
// Output
/*
[ { id: '0', events: [ 'usa' ] },
{ id: '1', events: [ 'spain', 'japan' ] },
{ id: '2', events: [ 'china' ] },
{ id: '3', events: [ 'korea', 'singapore' ] } ]
*/
// Same using reduce
const orders2 = lines.reduce((all, item) => {
let [_, orderID, type] = item.split(";");
if (!all[orderID]) {
all[orderID] = {
id: orderID,
events: []
};
}
all[orderID].events.push(type);
return all;
}, []);
console.log(orders2);

最新更新