i am using whatsapp chat export .i made it as a Json.i want store continuos messages as one message in NodeJs

huangapple go评论113阅读模式
英文:

i am using whatsapp chat export .i made it as a Json.i want store continuos messages as one message in NodeJs

问题

我明白,以下是您要翻译的内容:

"i have a whatsapp chat export i want to make the continuos messages as single message"

"the messages are given below i want to make the continuos messages as single message"

"My NodeJs code"

"in this code i read the data from txt file a store in array"

"then the continuous messages should be placed under one object"

"this program only joins two messages only not join the third one"

"how do i join the third element in the object"

"const fs = require("fs");"

"var data = JSON.parse(fs.readFileSync("file.txt"));"

"var newData = [];"

"data.forEach((x, index, arr) => {"

"if (index < arr.length - 1) {"

"y = arr[index + 1];"

"xkey = Object.keys(x);"

"ykey = Object.keys(y);"

"//console.log(xkey, ykey);"

"if(xkey[0] == ykey[0]) {"

"x = { [xkey]: x[xkey] + " " + y[ykey] };"

"delete arr[index + 1];"

"}"

"newData.push(x);"

"}"

"});"

"console.log(newData);"

"This is the text file"

"The Output i got"

"The desired Output"

英文:

i have a whatsapp chat export i want to make the continuos messages as single message

the messages are given below i want to make the continuos messages as single message

My NodeJs code

in this code i read the data from txt file a store in array
`then the continuous messages should be placed under one object

this program only joins two messages only not join the third one

how do i join the third element in the object

  1. const fs = require(&quot;fs&quot;);
  2. var data = JSON.parse(fs.readFileSync(&quot;file.txt&quot;));
  3. var newData = [];
  4. data.forEach((x, index, arr) =&gt; {
  5. if (index &lt; arr.length - 1) {
  6. y = arr[index + 1];
  7. xkey = Object.keys(x);
  8. ykey = Object.keys(y);
  9. //console.log(xkey, ykey);
  10. if(xkey[0] == ykey[0]) {
  11. x = { [xkey]: x[xkey] + &quot; &quot; + y[ykey] };
  12. delete arr[index + 1];
  13. }
  14. newData.push(x);
  15. }
  16. });
  17. console.log(newData);

This is the text file

  1. [
  2. { itachi: &#39;Sir&#39; },{ itachi: &#39;hi &#39; },{ itachi: &#39;hello&#39; },
  3. { Batman: &#39;hi&#39; },Batman: &#39;how r u&#39; },{ itachi: &#39;fine&#39; },
  4. ]

The Output i got

  1. [
  2. { itachi: &#39;Sir || hi&#39; },{ itachi: &#39;hello&#39; },
  3. { Batman: &#39;hi ||how r u&#39; },{ itachi: &#39;fine&#39; },
  4. ]

The desired Output

  1. { itachi: &#39;Sir || hi ||hello&#39; }, { Batman: &#39;hi ||how r u&#39; }, { itachi: &#39;fine&#39; },
  2. ]

答案1

得分: 1

我找到答案了:我将消息存储在下一个元素中,以便再次检查该值:

  1. var data = [
  2. { itachi: 'Sir' },{ itachi: 'hi ' },{ itachi: 'hello' },
  3. { Batman: 'hi' },{Batman: 'how r u' },{ itachi: 'fine' },]
  4. data.forEach((x, index, arr) => {
  5. if(index < arr.length - 1){
  6. y = arr[index + 1];
  7. xkey = Object.keys(x);
  8. ykey = Object.keys(y);
  9. //console.log(xkey, ykey);
  10. if(xkey[0] == ykey[0]) {
  11. x = { [xkey]: x[xkey] + " " + y[ykey] };
  12. arr[index]=""
  13. arr[index+1]=x
  14. }
  15. }
  16. })
  17. data=data.filter(x=>x!=="")
  18. console.log(data)

希望这有帮助。

英文:

I found the answer: I store the messages in the next element so that it will check the value again:

  1. var data = [
  2. { itachi: &#39;Sir&#39; },{ itachi: &#39;hi &#39; },{ itachi: &#39;hello&#39; },
  3. { Batman: &#39;hi&#39; },{Batman: &#39;how r u&#39; },{ itachi: &#39;fine&#39; },]
  4. data.forEach((x, index, arr) =&gt; {
  5. if(index &lt; arr.length - 1){
  6. y = arr[index + 1];
  7. xkey = Object.keys(x);
  8. ykey = Object.keys(y);
  9. //console.log(xkey, ykey);
  10. if(xkey[0] == ykey[0]) {
  11. x = { [xkey]: x[xkey] + &quot; &quot; + y[ykey] };
  12. arr[index]=&quot;&quot;
  13. arr[index+1]=x
  14. }
  15. }
  16. })
  17. data=data.filter(x=&gt;x!==&quot;&quot;)
  18. console.log(data)

答案2

得分: 0

你只需要检查最终的数值,因为最后一个数值在代码 index &lt; arr.length - 1 中被跳过。以下是代码的翻译部分:

  1. var data = [
  2. { itachi: '先生' },{ itachi: '嗨' },{ itachi: '你好' },
  3. { Batman: '嗨' },{Batman: '你好吗' },{ itachi: '好的' },
  4. ]
  5. var newData = [];
  6. data.forEach((x, index, arr) => {
  7. if(index < arr.length - 1){
  8. y = arr[index + 1];
  9. xkey = Object.keys(x);
  10. ykey = Object.keys(y);
  11. //console.log(xkey, ykey);
  12. if(xkey[0] == ykey[0]) {
  13. x = { [xkey]: x[xkey] + " " + y[ykey] };
  14. delete arr[index + 1];
  15. }
  16. newData.push(x);
  17. }else{
  18. newData.push(x);
  19. }
  20. });
  21. console.log(newData);

这是你提供的代码的中文翻译部分。

英文:

you just need to check the final value. because the last value is skipped with the code index &lt; arr.length - 1
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var data = [
  2. { itachi: &#39;Sir&#39; },{ itachi: &#39;hi &#39; },{ itachi: &#39;hello&#39; },
  3. { Batman: &#39;hi&#39; },{Batman: &#39;how r u&#39; },{ itachi: &#39;fine&#39; },
  4. ]
  5. var newData = [];
  6. data.forEach((x, index, arr) =&gt; {
  7. if(index &lt; arr.length - 1){
  8. y = arr[index + 1];
  9. xkey = Object.keys(x);
  10. ykey = Object.keys(y);
  11. //console.log(xkey, ykey);
  12. if(xkey[0] == ykey[0]) {
  13. x = { [xkey]: x[xkey] + &quot; &quot; + y[ykey] };
  14. delete arr[index + 1];
  15. }
  16. newData.push(x);
  17. }else{
  18. newData.push(x);
  19. }
  20. });
  21. console.log(newData);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月14日 23:14:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定