英文:
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
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' },
]
答案1
得分: 1
我找到答案了:我将消息存储在下一个元素中,以便再次检查该值:
var data = [
{ itachi: 'Sir' },{ itachi: 'hi ' },{ itachi: 'hello' },
{ Batman: 'hi' },{Batman: 'how r u' },{ itachi: 'fine' },]
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] };
arr[index]=""
arr[index+1]=x
}
}
})
data=data.filter(x=>x!=="")
console.log(data)
希望这有帮助。
英文:
I found the answer: I store the messages in the next element so that it will check the value again:
var data = [
{ itachi: 'Sir' },{ itachi: 'hi ' },{ itachi: 'hello' },
{ Batman: 'hi' },{Batman: 'how r u' },{ itachi: 'fine' },]
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] };
arr[index]=""
arr[index+1]=x
}
}
})
data=data.filter(x=>x!=="")
console.log(data)
答案2
得分: 0
你只需要检查最终的数值,因为最后一个数值在代码 index < arr.length - 1
中被跳过。以下是代码的翻译部分:
var data = [
{ itachi: '先生' },{ itachi: '嗨' },{ itachi: '你好' },
{ Batman: '嗨' },{Batman: '你好吗' },{ itachi: '好的' },
]
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);
}else{
newData.push(x);
}
});
console.log(newData);
这是你提供的代码的中文翻译部分。
英文:
you just need to check the final value. because the last value is skipped with the code index < arr.length - 1
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var data = [
{ itachi: 'Sir' },{ itachi: 'hi ' },{ itachi: 'hello' },
{ Batman: 'hi' },{Batman: 'how r u' },{ itachi: 'fine' },
]
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);
}else{
newData.push(x);
}
});
console.log(newData);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论