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

311
Views
¿Cómo actualizar un objeto en la matriz si existe, de lo contrario, agregarlo como un nuevo objeto en la matriz en javascript?

Tengo una variedad de objetos.

 const target = [{name: 'srk@gmail.com', selected: false, alertType: 'max'}, {name: 'abc@gmail.com', selected: true, alertType: 'clear'},]

ahora quiero actualizar la matriz según el nombre y el tipo de alerta. por ejemplo: si el nombre y el tipo de alerta coinciden, actualice la propiedad seleccionada. si eso no coincide, agregue un nuevo Objeto. nuevo objeto es

 const = {name: 'srk@gmail.com', selected: false, alertType: 'both'}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar Array.find() para encontrar elementos coincidentes. Luego actualizaría si se ha encontrado un elemento existente, o agregaría uno nuevo si no:

 const target = [ {name: 'srk@gmail.com', selected: false, alertType: 'max'}, {name: 'abc@gmail.com', selected: true, alertType: 'clear'} ]; const update = { name: 'srk@gmail.com', selected: false, alertType: 'both' }; function updateAlerts(input, update) { let foundAlert = input.find(alert => ((alert.alertType === update.alertType) && (alert.name === update.name))); if (foundAlert) { foundAlert.selected = update.selected; } else { input.push(update); } } updateAlerts(target, { name: 'srk@gmail.com', selected: false, alertType: 'both' }) console.log(target); updateAlerts(target, { name: 'srk@gmail.com', selected: true, alertType: 'max' }) console.log(target);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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!