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

150
Views
Recorriendo dos matrices de objetos para realizar un bucle para identificar identificadores coincidentes. Luego devuelva una nueva matriz de objetos personalizados basados en valores personalizados

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

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!