How to add new objects inside array of objects


#1

Hello everyone, i’ve been practicing with objects lately… i just want to ask if is this correct way of adding new objects inside the array of objects

var bob = {
firstName: “Bob”,
lastName: “Fisher”,
age: 55,
gender: “Male”,
phoneNumber: “(650) 888-8888”,
email: "bobfisher@example.com"
};

var mary = {
firstName: “Mary”,
lastName: “Winchester”,
age: 45,
gender: “Female”,
phoneNumber: “(655) 888-8888”,
email: "marywc@example.com"
};

var dean = {
firstName: “Dean”,
lastName: “Winchester”,
age: 30,
gender: “Male”,
phoneNumber: “(653) 888-8888”,
email: "deanwc@example.com"
};

var sam = {
firstName: “Samn”,
lastName: “Winchester”,
age: 30,
gender: “Male”,
phoneNumber: “(652) 888-8888”,
email: "samwc@example.com"
};

var contacts = [bob,mary,dean,sam];
var found = false;

/* Adding more people to contacts */
function newContact(firstName,lastName,age,gender,phoneNumber,email){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
this.phoneNumber = phoneNumber;
this.email = email;
}
var person = new newContact(“Higgs”,“Winchester”,150,“N/A”,“555-5555”,“N/A”);
contacts.push(person);
console.log(contacts);

/* Print found match */
function printPerson(person){
console.log(person.firstName + " " + person.lastName + ": " + person.phoneNumber);
}

/* Search function */
function search(lastName){
for(var i=0;i<contacts.length;i++){
if(lastName === contacts[i].lastName){
printPerson(contacts[i]);
found = true;
}
}
}

search(“Winchester”);

/* fallback */
if(!found){
console.log(“No match found”);
}


#2

I think what you might want to look into is something called constructors. You can declare one at the top of your code, for example, a person constructor as follows.

//declares the empty contacts array
var contacts = [];

function Person(first, last, age, gender, phone, email){
    //this creates the constructor that generates "person" objects
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.gender = gender;
    this.phoneNumber = phone;
    this.email = email;

    //defines the method that adds them to the Contacts array
    this.addToContacts = function(){
        contacts.push(this);
}

From there, you can declare sam, dean, mary, and bob as “Person Objects” as follows:

var bob = new Person( 'Bob' , 'Fisher' , 55 , 'Male' , '650-888-8888' , 'bobfisher@example.com');
var mary = new Person('Mary','winchester','female','888-888-8888','test@example.com');
//so on so forth
//and whenever you want to add someone to the contact list, you can call:
bob.addToContacts();
mary.addToContacts();
//this will add them to the contacts array, where you can then loop and search through.

I can’t promise that this code is perfect, but it should help cut down the amount of code you do end up typing! Have fun, and happy coding


#4

this is really helpful thanks!!!


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.