Advertisement
sazid_iiuc

contactClass

Feb 2nd, 2021
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Contact{
  2.     constructor() {
  3.         this.contacts = []
  4.     }
  5.  
  6.     getAllContacts(){
  7.         return this.contacts;
  8.     }
  9.  
  10.     getContactsByID(id){
  11.         return this.contacts.find(contact => contact.id === id);
  12.     }
  13.  
  14.     createContact(contact){
  15.         contact.id = this.contacts.length + 1;
  16.         this.contacts.push(contact);
  17.     }
  18.  
  19.     updateContactByID(id, updatedValue){
  20.         let index = this.contacts.findIndex(contact => contact.id === id);
  21.  
  22.         this.contacts[index].name = updatedValue.name || this.contacts.name;
  23.         this.contacts[index].phone = updatedValue.phone || this.contacts.phone;
  24.  
  25.         return this.contacts[index];
  26.     }
  27.  
  28.     deleteContactByID(id){
  29.         let index = this.contacts.findIndex(contact => contact.id === id);
  30.         this.contacts = this.contacts.filter(contact => contact.id !== id);
  31.     }
  32.  
  33. }
  34.  
  35.  
  36. module.exports = new Contact()
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement