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
[
{ itachi: 'Sir' },{ itachi: 'hi ' },{ itachi: 'hello' },
{ Batman: 'hi' },Batman: 'how r u' },{ itachi: 'fine' },
]
The Output i got
[
{ itachi: 'Sir || hi' },{ itachi: 'hello' },
{ Batman: 'hi ||how r u' },{ itachi: 'fine' },
]
The desired Output
{ itachi: 'Sir || hi ||hello' }, { Batman: 'hi ||how r u' }, { itachi: 'fine' },
]
2
Answers
I found the answer: I store the messages in the next element so that it will check the value again:
you just need to check the final value. because the last value is skipped with the code
index < arr.length - 1