tengo
obj1= [{keyID: 'c71cc89335565241f59ac9bb9da162a3ddf4a6b483bdc06dd82b9275898ba5ee', name: 'user1@mail.com'}, {keyID: '34051d2554ab20b0d24c2c39946d96da7e07508e7a8546bc2df299601dcbff05', name: 'user2@mail.com'}]Y
obj2=[{id: 19, email: 'user4@mail.com', other_field: false}, {id: 221, email: 'user3@mail.com', other_field: false}, {id: 13, email: 'user2@mail.com', other_field: false)}, {id: 2, email: 'user1@mail.com', other_field: true] Como puede ver, tengo para obj1 keyID que es diferente de obj2 ID y eso está bien. Lo que quiero agruparlos por nombre (obj1) y correo electrónico (obj2) el campo con el mismo correo electrónico debe crear un nuevo objeto con la misma estructura obj2 con un nuevo campo " isPresent "
result=[{id: 19, email: 'user4@mail.com', other_field: false,isPresent=false}, {id: 221, email: 'user3@mail.com', other_field: false,isPresent=false}, {id: 13, email: 'user2@mail.com', other_field: false,isPresent=true)}, {id: 2, email: 'user1@mail.com', other_field: true,isPresent=true]¿Cómo puedo lograr eso?
Puede mapear sobre obj2 y verificar si existe un elemento con el mismo nombre en obj1 a través de algún archivo .
También distribuí el obj dentro del mapa en lugar de asignar isPresent directamente para evitar efectos secundarios.
const obj1 = [{ keyID: 'c71cc89335565241f59ac9bb9da162a3ddf4a6b483bdc06dd82b9275898ba5ee', name: 'user1@mail.com' }, { keyID: '34051d2554ab20b0d24c2c39946d96da7e07508e7a8546bc2df299601dcbff05', name: 'user2@mail.com' }]; const obj2 = [{ id: 19, email: 'user4@mail.com', other_field: false }, { id: 221, email: 'user3@mail.com', other_field: false }, { id: 13, email: 'user2@mail.com', other_field: false }, { id: 2, email: 'user1@mail.com', other_field: true }]; const result = obj2.map((obj) => ({ ...obj, isPresent: obj1.some(({ name }) => name === obj.email), })); console.log(result);