Hola, todavía estoy trabajando en mis habilidades de JS y tengo problemas para entender este problema. Tengo dos matrices, una de contactos telefónicos y otra de mensajes. Estoy tratando de recorrer cada uno y devolver una matriz personalizada con un nuevo objeto basado en la matriz de contactos.
const myNumber = '69'; const contacts = [ { phoneNumber: '420', name: "Mike Smith", favorite: false, color: "orange" }, { phoneNumber: '360', name: "John Smith", favorite: true, color: "green" }, ]; const messages = [ // ! from john to me { to: '69', from: '360', isRead: false, time: new Date(), owner: 0, message: 'Hello there friend this is walker' }, { to: '69', from: '360', isRead: true, time: new Date(), owner: 1, message: 'Hello there friend this is walker' }, // ! from random number to me { to: '69', from: '720', isRead: false, time: new Date(), owner: 0, message: 'Hello there friend from random person' }, { to: '69', from: '720', isRead: true, time: new Date(), owner: 1, message: 'Hello there friend from random person' } ];Me gustaría devolver una nueva matriz de mensajes que se vea así...
const newMessages = [ // ! from John to me { to: '69', from: '360', isRead: false, time: new Date(), owner: 0, message: 'Hello there friend this is John', displayName: "John Smith" }, { to: '69', from: '360', isRead: true, time: new Date(), owner: 1, message: 'Hello there friend this is John', displayName: "John Smith" }, // ! from random number to me { to: '69', from: '720', isRead: false, time: new Date(), owner: 0, message: 'Hello there friend from random person', displayName: '720', }, { to: '69', from: '720', isRead: true, time: new Date(), owner: 1, message: 'Hello there friend from random person', displayName: '720', } ];Agregamos la propiedad de nombre para mostrar a los nuevos objetos de mensajes. El nombre para mostrar devolverá el nombre del contacto si "para" | "de" valor == el contacto "número de teléfono". Si este no es el caso, el nombre para mostrar de forma predeterminada devolverá el valor del mensaje "de".
Realmente no estoy seguro de cómo hacer esto. Traté de mirar algunos otros hilos pero no estoy teniendo suerte. Por favor, hágamelo saber si tiene algún consejo. Además, si esta explicación no fue útil, ¡házmelo saber! ¡Gracias!
Puede map sobre la matriz y usar Array.find para obtener el elemento en los contacts cuya propiedad phoneNumber es la propiedad del elemento actual to o from . Si se encuentra un elemento, asigne la propiedad de name del elemento a la propiedad displayName del elemento actual:
Para implementar un nombre para mostrar predeterminado, podemos eliminar la instrucción if y usar un operador ternario para asignar la propiedad de name si se encontró un elemento y, de lo contrario, asignar el nombre para mostrar predeterminado.
const myNumber = '69'; const contacts=[{phoneNumber:"420",name:"Mike Smith",favorite:!1,color:"orange"},{phoneNumber:"360",name:"John Smith",favorite:!0,color:"green"}]; const messages=[{to:"69",from:"360",isRead:!1,time:new Date,owner:0,message:"Hello there friend this is walker"},{to:"69",from:"360",isRead:!0,time:new Date,owner:1,message:"Hello there friend this is walker"},{to:"69",from:"720",isRead:!1,time:new Date,owner:0,message:"Hello there friend from random person"},{to:"69",from:"720",isRead:!0,time:new Date,owner:1,message:"Hello there friend from random person"}]; let defaultDisplayName = 'Spectric' const result = messages.map(e => { let found = contacts.find(f => [e.to, e.from].includes(f.phoneNumber)) e.displayName = found ? found.name : defaultDisplayName; return e; }) console.log(result)