Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

350
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda