Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
two txt files data how to getting and merge based on customerid how to getting the two txt files data using with node js?

i am newly learner nodejs developer

i have two txt files like file1.txt and file2.txt file1.text data is different and file2.txt data is different but customerid is same beased on customerid id how to getting two files data

file1.txt data is:
[{'customerid':12,
'name':'xyz',
'email':'test@gmail.com'
},
{'customerid':14,
'name':'def',
'email':'def@gmail.com'
},
{'customerid':15,
'name':'pqr',
'email':'pqr@gmail.com'}
,{'customerid':17,
'name':'abc',
'email':'abc@gmail.com'}]

file2.txt data is:
[{
' customerid':12,
 'vachilenum':26789,
 'price':390900920,
 "phone":673648494894
 },
 {'customerid':14,
 'vachilenum':26789,
 'price':390900920,
 "phone":673648494894
 },
 {'customerid':15,
 'vachilenum':26789,
 'price':390900920,
 "phone":673648494894
 },
 {'customerid':17,
 'vachilenum':26789,
 'price':390900920,
 "phone":673648494894
 }];

i am trying this code but iam not getting expected out put any one please suggest me

const arr1 = fs.readFileSync('file1.txt', 'utf8').toString();
console.log(arr1)
const arr2 = fs.readFileSync('file2.text', 'utf8').toString();
console.log(arr2)

const merge = (arr1, arr2) => {
  const temp = []

  arr1.forEach(x => {
    arr2.forEach(y => {
      if (x.id === y.id) {
        temp.push({ ...x, ...y })
      }
    })
  })

  return temp
}
console.log(merge(arr1, arr2))




   expected outputt is
       {'customerid':12,
        'name':'xyz',
        'email':'test@gmail.com'
        'vachilenum':26789,
         'price':390900920,
         "phone":673648494894
        },
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to use the map function in combination with find and return the merged object.
Also, you may wanna parse the file content to a JSON object:

const arr1 = JSON.parse(fs.readFileSync('file1.txt', 'utf8'));
console.log(arr1);
const arr2 = JSON.parse(fs.readFileSync('file2.text', 'utf8'));
console.log(arr2);

const merge = (arr1, arr2) => {
  return arr1.map(x => {
    const y = arr2.find(val => val.customerid === x.customerid);
    if (!y) return x;
    return { ...x, ...y }
  })
};

console.log(merge(arr1, arr2));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!