How does it work? "be able to refer to it by its array position, no need for a direct name"


#1


I don't understand how this works with my rest of the code.

I wish to hear some line by line explanation for this concept:

Lesson) We can do the insert in a succinct way by adding the new object directly into the array position without even giving it a name. This can be confusing, but we will be able to refer to it by its array position, so it does not need a direct name. Do it like this:

Also the instruction 3). Which line of code is doing below?

3) Add this new contact object to the contacts array.


contacts[contacts.length] = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
};


following is my code:

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

function list() {
	var contactsLength = contacts.length;
	for (var i = 0; i < contactsLength; i++) {
		printPerson(contacts[i]);
	}
}

/*Create a search function
then call it passing "Jones"*/
function search (lastName) {
    var contactsLength = contacts.length;
    for (var i = 0; i<contactsLength; i++){
        if (lastName === contacts[i]["lastName"]){
            console.log(printPerson(contacts[i]));
        }
    }
};

search("Jones");
function add (firstName, lastName, email, phoneNumber){
    contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
    };

}

add("Heggy", "Here", "heggy@gmail.com", "(324) 345-2233");
list();

#2

Your contacts array starts out with two elements, [0] and [1] are their indices. They do not contain the objects, though, only references to bob and mary, which physical objects are in global memory.

contacts = [ bob, mary ]

This is not a very efficient way to do it, as named objects have no association to each other and would pile up rather quickly and be subject to destruction. That is why we create the objects right in the array. Now they are all in one place, and associated by sharing a common array.

To add an element to the end of the array, we assign it the way you have,

contacts[contacts.length] = {}

Arrays are zero-indexed, so contacts.length will be the index of the first non-existent element, to which we can assign the new object. In this case, that will be contacts[2], and contacts.length will now be 3.


#3

For the fun of it, go back to that lesson and at the end, change bob to "the meaning of life" and mary to 42. Now query your array.

[ 'the meaning of life', 42, { firstName: 'Heggy', lastName: 'Here', phoneNumber: '(324) 345-2233', email: 'heggy@gmail.com' }]

#4

I realized there are two paths to this solution.
1) create a regular function and inside it has array[position] = object. > //We can do the insert in a succinct way by adding the new object directly into the array position without even giving it a name.

function add (firstName, lastName, email, phoneNumber){
    contacts[contacts.length] = {  //append new contact entry at the position one after the last item of my contact array
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
    };
}

2) create a constructor then use push method.
- constructor remember to capitalize first letter of constructor name for convention

function Person (firstName, lastName, phoneNumber, email){
   this.firstName = firstName;  //this key word makes it constructor
   this.lastName = lastName;
   this.phoneNumber = phoneNumber;
   this.email = email;
}
  • add function with push method while referring back to the above constrctor

function add (firstName, lastName, phoneNumber, email){
   var info = new Person (firstName, lastName, phoneNumber, email);  //use new keyword when creating new entry using constructor 
   contact.push(info);
}

#5

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