简体   繁体   English

合并所有数据,直到JavaScript中遇到分号为止

[英]merge all data until semicolon encountered in JavaScript

I am trying to merge all the data in multiple lines until I encounter semicolon (;). 我试图将所有数据合并成多行,直到遇到分号(;)。

input 输入

1
09;

8
9;

10
9;

1
00
;

2
0;

output 产量

109
89
109
100
20

How do I achieve this using JavaScript? 如何使用JavaScript实现此目标?

This data is not static but dynamic, data is coming in real-time and I need to process one data values until semicolon and push it to browser front-end using sockets. 这些数据不是静态的而是动态的,数据是实时的,我需要处理一个数据值,直到分号为止,然后使用套接字将其推送到浏览器前端。 so I am not in need to process all data at once and then push it. 因此我不需要一次处理所有数据然后将其推送。

Doing this: 这样做:

console.log("before: ",receivedData);
console.log("after: ",receivedData.split('\n').join('').split(';').join('\n'));

results in : 结果是 :

after:
before:  2
after:  2
before:  05
after:  05
before:  ;
after:

before:

after:
before:  2
after:  2
before:  12
after:  12
before:  ;
after:

new data comes in every half a second (500ms). 每隔半秒(500毫秒)就会收到一次新数据。 I am not sure if its too fast for thing to process? 我不确定它是否太快而无法处理? its just real-time I guess so there is no time for computation? 我想它只是实时的,所以没有时间进行计算了吗?

The code you are looking for is probably somthing like that: 您正在寻找的代码可能是这样的:

 var buffer = ''; function process(data){ var separatorIndex; while((separatorIndex = data.indexOf(';')) !== -1){ send(buffer + data.substr(0, separatorIndex)); buffer = ''; data = data.substr(separatorIndex + 1); } buffer += data; } function send(data){ console.log(data); } process('123'); process('456;789;123'); process(';'); 

If you could accumulate all data and transform it at once, test.replace(/[^0-9;]|;$/g, '').replace(/;/g, '\\r\\n') should do the trick. 如果您可以累积所有数据并立即进行转换,请执行test.replace(/[^0-9;]|;$/g, '').replace(/;/g, '\\r\\n')诀窍。 The first regexp removes everything which is not a number or a semicolon (and just in case the semicolon at the end), the second call replaces all semicolons by line breaks. 第一个正则表达式删除不是数字或分号的所有内容(以防万一,请在末尾使用分号),第二个正则表达式将所有分号替换为换行符。

Add a string representing your data. 添加一个代表您的数据的字符串。 Remember to escape (use backslashes) at the end of every line to make it run on (alternatively, use a string template, as I have done below:) 请记住在每一行的末尾转义(使用反斜杠)以使其继续运行(或者,使用字符串模板,如我在下面所做的那样:)

const data =`
1
09;

8
9;

10
9;

1
00
;

2
0;`
const target = []; //Where our results will end up.

Then, use a for loop over the data: 然后,对数据使用for循环:

var current = 0;
for (var iterator = 0; iterator > data.length; iterator++) {
    if (!target[current]) {target[current] = '';}
    if (data[iterator] != ";" && data[iterator] != "\n") {
        target[current] += data[iterator]; //Add current data to the target
        continue; //Continue the loop.
    }  
    else if (data[iterator] == ";") {
         current++; //Add one to current, so that further text ends up in the next index
         continue; //Continue the loop.
    }
    else {
         continue;
    }
}

Of course, this isn't efficent , but it will work, and if you are new to JS, you will be going down this route. 当然,这不是有效的方法 ,但是它可以工作,如果您不熟悉JS,那么您会沿这条路走。 String manipulation is a task with varying difficulty levels. 字符串操作是一项具有不同难度级别的任务。 You can also use regular expressions or .join and .split chains to achieve the same as this loop. 您也可以使用正则表达式或.join和.split链来实现与此循环相同的效果。

Remove all the whitespace, then split on ; 删除所有空格,然后分割;

 var str =`1 09; 8 9; 10 9; 1 00 ; 2 0;` var res = str.replace(/\\s/g, '').split(';').join('\\n') console.log(res) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM