Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

352
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda